你如何在 Wordpress 上使用 JSON API in PHP?
How do you use a JSON API in PHP on Wordpress?
这是我 url https://goalserve.com/getfeed/mytoken/topscorers/1204?json=1
的地址
如何获取“球员”数据,我想在我的 PHP Wordpress 网站上使用制作“最佳射手站立小部件”。
{
"?xml":{
"@version":"1.0",
"@encoding":"utf-8"
},
"topscorers":{
"@sport":"soccer",
"tournament":{
"@name":"Premier League",
"@stage_id":"12041081",
"@gid":"1204",
"@is_current":"True",
"@id":"1204",
"player":[
{
"@pos":"1",
"@name":"Mohamed Salah",
"@team":"Liverpool",
"@team_id":"9249",
"@goals":"15",
"@penalty_goals":"2",
"@id":"138653"
},
{
"@pos":"2",
"@name":"Diogo Jota",
"@team":"Liverpool",
"@team_id":"9249",
"@goals":"10",
"@penalty_goals":"0",
"@id":"374031"
},
{
"@pos":"3",
"@name":"J. Vardy",
"@team":"Leicester City",
"@team_id":"9240",
"@goals":"9",
"@penalty_goals":"0",
"@id":"159732"
}
]
}
}
}
回答
https://www.php.net/manual/en/function.json-decode.php
为此,您必须首先将 json 字符串解码为 php 对象。然后您必须向下钻取到您想要的正确数据。您可以像这样使用 ->
运算符来执行此操作:
$players = json_decode($raw_string)->{'topscorers'}->{'tournament'}->{'player'};
请注意,如果您打算重复使用数据,则应保存解码 json 字符串的结果。不要多次解码。
获得球员后,您可以遍历数据并用它做您想做的事。这是一个包含您的数据的最小示例,因此您可以看到它们协同工作。
例子
数据集
<?php
$raw_string = '
{
"?xml":{
"@version":"1.0",
"@encoding":"utf-8"
},
"topscorers":{
"@sport":"soccer",
"tournament":{
"@name":"Premier League",
"@stage_id":"12041081",
"@gid":"1204",
"@is_current":"True",
"@id":"1204",
"player":[
{
"@pos":"1",
"@name":"Mohamed Salah",
"@team":"Liverpool",
"@team_id":"9249",
"@goals":"15",
"@penalty_goals":"2",
"@id":"138653"
},
{
"@pos":"2",
"@name":"Diogo Jota",
"@team":"Liverpool",
"@team_id":"9249",
"@goals":"10",
"@penalty_goals":"0",
"@id":"374031"
},
{
"@pos":"3",
"@name":"J. Vardy",
"@team":"Leicester City",
"@team_id":"9240",
"@goals":"9",
"@penalty_goals":"0",
"@id":"159732"
}
]
}
}
}';
?>
处理中
<?php
$php_object = json_decode($raw_string);
$players = $php_object->{'topscorers'}->{'tournament'}->{'player'};
$html = '';
$html .= '<table>';
$html .= '<tr>';
$html .= '<td>Pos</td>';
$html .= '<td>Player</td>';
$html .= '<td>Team</td>';
$html .= '<td>Goals</td>';
$html .= '</tr>';
foreach ($players as $p) {
$html .= '<tr>';
$html .= '<td>' . $p->{'@pos'} . '</td>';
$html .= '<td>' . $p->{'@name'} . '</td>';
$html .= '<td>' . $p->{'@team'} . '</td>';
$html .= '<td>' . $p->{'@goals'} . '</td>';
$html .= '</tr>';
}
$html .= '</table>';
echo($html);
?>
输出
<table><tr><td>Pos</td><td>Player</td><td>Team</td><td>Goals</td></tr><tr><td>1</td><td>Mohamed Salah</td><td>Liverpool</td><td>15</td></tr><tr><td>2</td><td>Diogo Jota</td><td>Liverpool</td><td>10</td></tr><tr><td>3</td><td>J. Vardy</td><td>Leicester City</td><td>9</td></tr></table>
Pos
Player
Team
Goals
1
Mohamed Salah
Liverpool
15
2
Diogo Jota
Liverpool
10
3
J. Vardy
Leicester City
9
这是我 url https://goalserve.com/getfeed/mytoken/topscorers/1204?json=1
的地址如何获取“球员”数据,我想在我的 PHP Wordpress 网站上使用制作“最佳射手站立小部件”。
{
"?xml":{
"@version":"1.0",
"@encoding":"utf-8"
},
"topscorers":{
"@sport":"soccer",
"tournament":{
"@name":"Premier League",
"@stage_id":"12041081",
"@gid":"1204",
"@is_current":"True",
"@id":"1204",
"player":[
{
"@pos":"1",
"@name":"Mohamed Salah",
"@team":"Liverpool",
"@team_id":"9249",
"@goals":"15",
"@penalty_goals":"2",
"@id":"138653"
},
{
"@pos":"2",
"@name":"Diogo Jota",
"@team":"Liverpool",
"@team_id":"9249",
"@goals":"10",
"@penalty_goals":"0",
"@id":"374031"
},
{
"@pos":"3",
"@name":"J. Vardy",
"@team":"Leicester City",
"@team_id":"9240",
"@goals":"9",
"@penalty_goals":"0",
"@id":"159732"
}
]
}
}
}
回答
https://www.php.net/manual/en/function.json-decode.php
为此,您必须首先将 json 字符串解码为 php 对象。然后您必须向下钻取到您想要的正确数据。您可以像这样使用 ->
运算符来执行此操作:
$players = json_decode($raw_string)->{'topscorers'}->{'tournament'}->{'player'};
请注意,如果您打算重复使用数据,则应保存解码 json 字符串的结果。不要多次解码。
获得球员后,您可以遍历数据并用它做您想做的事。这是一个包含您的数据的最小示例,因此您可以看到它们协同工作。
例子
数据集
<?php
$raw_string = '
{
"?xml":{
"@version":"1.0",
"@encoding":"utf-8"
},
"topscorers":{
"@sport":"soccer",
"tournament":{
"@name":"Premier League",
"@stage_id":"12041081",
"@gid":"1204",
"@is_current":"True",
"@id":"1204",
"player":[
{
"@pos":"1",
"@name":"Mohamed Salah",
"@team":"Liverpool",
"@team_id":"9249",
"@goals":"15",
"@penalty_goals":"2",
"@id":"138653"
},
{
"@pos":"2",
"@name":"Diogo Jota",
"@team":"Liverpool",
"@team_id":"9249",
"@goals":"10",
"@penalty_goals":"0",
"@id":"374031"
},
{
"@pos":"3",
"@name":"J. Vardy",
"@team":"Leicester City",
"@team_id":"9240",
"@goals":"9",
"@penalty_goals":"0",
"@id":"159732"
}
]
}
}
}';
?>
处理中
<?php
$php_object = json_decode($raw_string);
$players = $php_object->{'topscorers'}->{'tournament'}->{'player'};
$html = '';
$html .= '<table>';
$html .= '<tr>';
$html .= '<td>Pos</td>';
$html .= '<td>Player</td>';
$html .= '<td>Team</td>';
$html .= '<td>Goals</td>';
$html .= '</tr>';
foreach ($players as $p) {
$html .= '<tr>';
$html .= '<td>' . $p->{'@pos'} . '</td>';
$html .= '<td>' . $p->{'@name'} . '</td>';
$html .= '<td>' . $p->{'@team'} . '</td>';
$html .= '<td>' . $p->{'@goals'} . '</td>';
$html .= '</tr>';
}
$html .= '</table>';
echo($html);
?>
输出
<table><tr><td>Pos</td><td>Player</td><td>Team</td><td>Goals</td></tr><tr><td>1</td><td>Mohamed Salah</td><td>Liverpool</td><td>15</td></tr><tr><td>2</td><td>Diogo Jota</td><td>Liverpool</td><td>10</td></tr><tr><td>3</td><td>J. Vardy</td><td>Leicester City</td><td>9</td></tr></table>
Pos | Player | Team | Goals |
---|---|---|---|
1 | Mohamed Salah | Liverpool | 15 |
2 | Diogo Jota | Liverpool | 10 |
3 | J. Vardy | Leicester City | 9 |