Home > Blockchain >  How do you use a JSON API in PHP on Wordpress?
How do you use a JSON API in PHP on Wordpress?

Time:12-31

Here my url address for https://goalserve.com/getfeed/mytoken/topscorers/1204?json=1

How can I get the "player" data, I want to use make "top scorers standing widget" on my PHP Wordpress site.

{
    "?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"
                }
            ]
        }
    }
}

CodePudding user response:

Answer

https://www.php.net/manual/en/function.json-decode.php

To do this you must first decode the json string into a php object. Then you must drill down to the proper data that you want. You can do this with the -> operator like so:

$players = json_decode($raw_string)->{'topscorers'}->{'tournament'}->{'player'};

Note that you should save the result of the decoded json string if you plan on reusing the data. Don't decode it more than once.

After you have the players, you can then iterate over the data and do what you want with it. Here is a minimal example with your data so you can see it all working together.

Example

Dataset

<?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"
                }
            ]
        }
    }
}';
?>

Processing

<?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);

?>

Output

<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
  • Related