【问题标题】:How do you use a JSON API in PHP on Wordpress?您如何在 Wordpress 上的 PHP 中使用 JSON API?
【发布时间】:2021-12-30 08:33:00
【问题描述】:

这是我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"
                }
            ]
        }
    }
}

【问题讨论】:

  • 到目前为止你尝试了什么?
  • 我尝试使用goalserve.com/getfeed/mytoken/topscorers/1204?json=1 调用 API,但仅显示来自“?xml”和“topscorers”的 2 个数据。我怎样才能得到“球员”部分,因为它在“顶级得分手”->“锦标赛”下。
  • 您是在客户端还是服务器端进行操作
  • 我对客户端或服务器端一无所知。我试图为 foreach 循环获取“播放器”,我现在这样做仍然为“?xml”和“topscorers”数据卡住了 foreach。
  • 你使用的是javascript还是php?

标签: php json wordpress api associative-array


【解决方案1】:

回答

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

【讨论】:

  • 太棒了!!你救了我的命,我已经尝试了这个问题一个小时。非常感谢。
猜你喜欢
  • 1970-01-01
  • 2012-04-13
  • 2016-11-17
  • 1970-01-01
  • 2014-10-31
  • 1970-01-01
  • 2018-04-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多