【问题标题】:Parsing XML using JSON PHP and outputting data使用 JSON PHP 解析 XML 并输出数据
【发布时间】:2013-11-24 21:03:40
【问题描述】:

我正在尝试在 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

这是一个示例 URL 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>

【问题讨论】:

  • 您无法将 xml 文件解析为 json 结构。请改用 simplexml_load_file。 go to php manual

标签: php xml json


【解决方案1】:

使用 simplexml。

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

【讨论】:

  • 将此添加到我的代码中,我得到“字符串无法解析为 XML”
  • 也许它不是有效的 XML。很难通过样品来判断。反正不是json。
猜你喜欢
  • 1970-01-01
  • 2014-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-15
  • 1970-01-01
  • 2021-03-21
  • 2012-11-13
相关资源
最近更新 更多