使用JSON PHP解析XML并输出数据

时间:2013-11-24 21:03:41

标签: php xml json

我正在尝试用PHP解析一些xml,并且可以构建正确的url来获取数据,但我仍然坚持确定如何查看结构的过程,以便我可以避免这样的错误

Notice: Trying to get property of non-object in C:\wamp\www\sportchecker\soccer.php on line 39
Call Stack
#   Time    Memory  Function    Location
1   0.0020  252360  {main}( )   ..\sports.php:0

以下是一个示例网址http://api.pinnaclesports.com/v1/leagues?sportid=29,是否可以告诉我如何在php代码中浏览树?我使用API​​密钥进行的调用返回了大量数据。这是我的代码

$url = "http://api.pinnaclesports.com/v1/feed?sportid=$sport_id&leagueid=$league_id&clientid=$client_id&apikey=$api_key&oddsformat=$oddsformat";
    $json_string = file_get_contents($url);
    $parsed_json = json_decode($json_string);

    // output the xml data
    $wSports = $parsed_json->sports->sport->id;
     echo "Status :  ${wSports} ";

     // test if url is correct
     echo $url;

编辑:这是一个数据样本,我试图让团队和时间回来。

<rsp status="ok">
<fd>
<fdTime>1385326075704</fdTime>
<sports>
<sport>
<id>29</id>
<leagues>
<league>
<id>1739</id>
<events>
<event>
<startDateTime>2013-11-25T22:59:00Z</startDateTime>
<id>334321824</id>
<IsLive>No</IsLive>
<status>I</status>
<drawRotNum>2056</drawRotNum>
<homeTeam type="Team1">
<name>Atletico Huracan</name>
<rotNum>2054</rotNum>
</homeTeam>
<awayTeam type="Team2">
<name>Talleres Cordoba</name>
<rotNum>2055</rotNum>
</awayTeam>
<periods>
<period lineId="117000156">
<number>0</number>
<description>Game</description>
<cutoffDateTime>2013-11-25T22:59:00Z</cutoffDateTime>
<spreads>
<spread>
<awaySpread>0.25</awaySpread>
<awayPrice>1.869</awayPrice>
<homeSpread>-0.25</homeSpread>
<homePrice>1.97</homePrice>
</spread>
</spreads>

1 个答案:

答案 0 :(得分:0)

使用simplexml。

$sports = new SimpleXMLElement($parsedJson);
$sports->sport[0]->id