douxi2670 2013-11-24 21:03
浏览 58

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

I am trying to parse some xml in PHP and can build the correct url to fetch the data, but I am stuck on the process of determining how to look at the structure so I can avoid errors like this

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

Here is an example URL http://api.pinnaclesports.com/v1/leagues?sportid=29, are their any tools that can tell me how to navigate through the tree in php code? The call I make with the API key has lots of data returned. Here is my code below

$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;

Edit: here is a sample of the data, I am trying to get the teams and time back.

<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条回答 默认 最新

  • duanaixuan7385 2013-11-24 21:08
    关注

    Use simplexml.

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

报告相同问题?

悬赏问题

  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用