【问题标题】:Reading XML and JSON in PHP在 PHP 中读取 XML 和 JSON
【发布时间】:2013-04-06 06:49:58
【问题描述】:

我正在尝试使用 PHP(我很陌生)从 API 读取一些数据。我对 JSON 不是很熟悉,也没有 XML 经验。那么,这是一个好的开始。

有问题的资源是:

http://scores.nbcsports.msnbc.com/ticker/data/gamesMSNBC.js.asp?jsonp=true&sport=MLB&period=20130413

看看这个页面的来源,它似乎是相当结构化的。我试图在这个 url 上同时使用 file_get_contentssimplexml_load_file 来看看会发生什么。在第一种情况下,我只收到浏览器中显示的文本(在我未经训练的眼睛看来,这就像一个空数组)。第二种情况完全失败('start tag expected')。

这些数据是什么格式的,我应该如何访问它?如何下拉数据,例如提取某些标签的值?

更新

我忘了提到我还尝试在网址上回显json_decode,但无济于事。没有任何回应。

【问题讨论】:

  • JSON 和 XML 当然是两个不同的东西。您可能正在使用来自 Ajax 的 JSON 或 XML(还有第三件事)。 PHP 有很好的 JSON 和 XML 库:PHP/JSONPHP/XML15-Minute PHP with XML Start。不幸的是,developerworks 链接目前因站点维护而关闭:(
  • 是的,我试图将其视为每个,但没有成功。我什至看不出我在处理什么。
  • 谢谢,这很有帮助。但我不确定如何提取全部内容,以便我可以尝试提取 JSON。
  • 另一个不错的 PHP/XML 教程:blog.teamtreehouse.com/how-to-parse-xml-with-php5。 PS:XML 总是这样:<some_tag>something</some_tag>; JSON 通常看起来更像这样:{ some_item : "moose"; } JSON 上的一个很好的链接:JSON Tutorial
  • 您实际拥有的是 JSONP,它是填充的 JSON,可以轻松地在 HTML 文档的 <script> 标记中使用。

标签: php xml json api


【解决方案1】:

您的链接包含以下内容:

http://scores.nbcsports.msnbc.com/ticker/data/gamesMSNBC.js.asp?jsonp=true&sport=MLB&period=20130413

shsMSNBCTicker.loadGamesData({
"sport": "MLB", 
"period": "20130413", 
"games": [
"<ticker-entry gamecode=\"330413105\" gametype=\"Regular Season\" league=\"AL\"><visiting-team display_name=\"Chi White Sox\" alias=\"CWS\" nickname=\"White Sox\" id=\"4\" division=\"ALC\" league=\"AL\" score=\"4\"><score heading=\"1\" value=\"2\"></score><score heading=\"2\" value=\"0\"></score><score heading=\"3\" value=\"0\"></score><score heading=\"4\" value=\"0\"></score><score heading=\"5\" value=\"0\"></score><score heading=\"6\" value=\"0\"></score><score heading=\"7\" value=\"2\"></score><score heading=\"8\" value=\"0\"></score><score heading=\"R\" value=\"4\"></score><score heading=\"H\" value=\"6\"></score><score heading=\"E\" value=\"0\"></score></visiting-team><home-team display_name=\"Cleveland\" alias=\"Cle\" nickname=\"Indians\" id=\"5\" division=\"ALC\" league=\"AL\" score=\"9\"><score heading=\"1\" value=\"2\"></score><score heading=\"2\" value=\"0\"></score><score heading=\"3\" value=\"1\"></score><score heading=\"4\" value=\"0\"></score><score heading=\"5\" value=\"6\"></score><score heading=\"6\" value=\"0\"></score><score heading=\"7\" value=\"0\"></score><score heading=\"R\" value=\"9\"></score><score heading=\"H\" value=\"9\"></score><score heading=\"E\" value=\"1\"></score></home-team><gamestate status=\"In-Progress\" display_status1=\"Top\" display_status2=\"8th\" href=\"http://scores.nbcsports.msnbc.com/mlb/boxscore.asp?gamecode=330413105\" tv=\"CSN/STOh\" gametime=\"4:05 PM\" gamedate=\"4/13\" is-dst=\"1\" is-world-dst=\"1\" reason=\"\"></gamestate></ticker-entry>"
,"<ticker-entry gamecode=\"330413110\" gametype=\"Regular Season\" league=\"AL\"><visiting-team display_name=\"Baltimore\" alias=\"Bal\" nickname=\"Orioles\" id=\"1\" division=\"ALE\" league=\"AL\" score=\"5\"><score heading=\"1\" value=\"1\"></score><score heading=\"2\" value=\"1\"></score><score heading=\"3\" value=\"2\"></score><score heading=\"4\" value=\"1\"></score><score heading=\"5\" value=\"0\"></score><score heading=\"6\" value=\"0\"></score><score heading=\"7\" value=\"0\"></score><score heading=\"R\" value=\"5\"></score><score heading=\"H\" value=\"10\"></score><score heading=\"E\" value=\"1\"></score></visiting-team><home-team display_name=\"NY Yankees\" alias=\"NYY\" nickname=\"Yankees\" id=\"10\" division=\"ALE\" league=\"AL\" score=\"3\"><score heading=\"1\" value=\"0\"></score><score heading=\"2\" value=\"2\"></score><score heading=\"3\" value=\"0\"></score><score heading=\"4\" value=\"0\"></score><score heading=\"5\" value=\"0\"></score><score heading=\"6\" value=\"1\"></score><score heading=\"7\" value=\"0\"></score><score heading=\"R\" value=\"3\"></score><score heading=\"H\" value=\"8\"></score><score heading=\"E\" value=\"1\"></score></home-team><gamestate status=\"In-Progress\" display_status1=\"Bot\" display_status2=\"7th\" href=\"http://scores.nbcsports.msnbc.com/mlb/boxscore.asp?gamecode=330413110\" tv=\"MASN/YES\" gametime=\"4:05 PM\" gamedate=\"4/13\" is-dst=\"1\" is-world-dst=\"1\" reason=\"\"></gamestate></ticker-entry>"
,"<ticker-entry gamecode=\"330413111\" gametype=\"Regular Season\" league=\"AL\"><visiting-team display_name=\"Detroit\" alias=\"Det\" nickname=\"Tigers\" id=\"6\" division=\"ALC\" league=\"AL\" score=\"7\"><score heading=\"1\" value=\"0\"></score><score heading=\"2\" value=\"0\"></score><score heading=\"3\" value=\"1\"></score><score heading=\"4\" value=\"4\"></score><score heading=\"5\" value=\"0\"></score><score heading=\"6\" value=\"2\"></score><score heading=\"7\" value=\"0\"></score><score heading=\"R\" value=\"7\"></score><score heading=\"H\" value=\"9\"></score><score heading=\"E\" value=\"0\"></score></visiting-team><home-team display_name=\"Oakland\" alias=\"Oak\" nickname=\"Athletics\" id=\"11\" division=\"ALW\" league=\"AL\" score=\"1\"><score heading=\"1\" value=\"0\"></score><score heading=\"2\" value=\"1\"></score><score heading=\"3\" value=\"0\"></score><score heading=\"4\" value=\"0\"></score><score heading=\"5\" value=\"0\"></score><score heading=\"6\" value=\"0\"></score><score heading=\"7\" value=\"0\"></score><score heading=\"R\" value=\"1\"></score><score heading=\"H\" value=\"3\"></score><score heading=\"E\" value=\"0\"></score></home-team><gamestate status=\"In-Progress\" display_status1=\"Bot\" display_status2=\"7th\" href=\"http://scores.nbcsports.msnbc.com/mlb/boxscore.asp?gamecode=330413111\" tv=\"FSDT/CSCA\" gametime=\"4:05 PM\" gamedate=\"4/13\" is-dst=\"1\" is-world-dst=\"1\" reason=\"\"></gamestate></ticker-entry>"
,"<ticker-entry gamecode=\"330413109\" gametype=\"Regular Season\" league=\"IL\"><visiting-team display_name=\"NY Mets\" alias=\"NYM\" nickname=\"Mets\" id=\"21\" division=\"NLE\" league=\"NL\" score=\"4\"><score heading=\"1\" value=\"0\"></score><score heading=\"2\" value=\"0\"></score><score heading=\"3\" value=\"0\"></score><score heading=\"4\" value=\"0\"></score><score heading=\"5\" value=\"4\"></score><score heading=\"6\" value=\"0\"></score><score heading=\"7\" value=\"0\"></score><score heading=\"8\" value=\"0\"></score><score heading=\"R\" value=\"4\"></score><score heading=\"H\" value=\"9\"></score><score heading=\"E\" value=\"0\"></score></visiting-team><home-team display_name=\"Minnesota\" alias=\"Min\" nickname=\"Twins\" id=\"9\" division=\"ALC\" league=\"AL\" score=\"1\"><score heading=\"1\" value=\"0\"></score><score heading=\"2\" value=\"0\"></score><score heading=\"3\" value=\"0\"></score><score heading=\"4\" value=\"0\"></score><score heading=\"5\" value=\"0\"></score><score heading=\"6\" value=\"0\"></score><score heading=\"7\" value=\"1\"></score><score heading=\"8\" value=\"0\"></score><score heading=\"R\" value=\"1\"></score><score heading=\"H\" value=\"1\"></score><score heading=\"E\" value=\"0\"></score></home-team><gamestate status=\"In-Progress\" display_status1=\"Bot\" display_status2=\"8th\" href=\"http://scores.nbcsports.msnbc.com/mlb/boxscore.asp?gamecode=330413109\" tv=\"SNY/FSN\" gametime=\"4:10 PM\" gamedate=\"4/13\" is-dst=\"1\" is-world-dst=\"1\" reason=\"\"></gamestate></ticker-entry>"
,"<ticker-entry gamecode=\"330413124\" gametype=\"Regular Season\" league=\"NL\"><visiting-team display_name=\"Milwaukee\" alias=\"Mil\" nickname=\"Brewers\" id=\"8\" division=\"NLC\" league=\"NL\" score=\"0\"><score heading=\"1\" value=\"0\"></score><score heading=\"2\" value=\"0\"></score><score heading=\"3\" value=\"0\"></score><score heading=\"4\" value=\"0\"></score><score heading=\"5\" value=\"0\"></score><score heading=\"6\" value=\"0\"></score><score heading=\"7\" value=\"0\"></score><score heading=\"R\" value=\"0\"></score><score heading=\"H\" value=\"3\"></score><score heading=\"E\" value=\"1\"></score></visiting-team><home-team display_name=\"St. Louis\" alias=\"StL\" nickname=\"Cardinals\" id=\"24\" division=\"NLC\" league=\"NL\" score=\"8\"><score heading=\"1\" value=\"0\"></score><score heading=\"2\" value=\"0\"></score><score heading=\"3\" value=\"1\"></score><score heading=\"4\" value=\"0\"></score><score heading=\"5\" value=\"0\"></score><score heading=\"6\" value=\"7\"></score><score heading=\"R\" value=\"8\"></score><score heading=\"H\" value=\"10\"></score><score heading=\"E\" value=\"0\"></score></home-team><gamestate status=\"In-Progress\" display_status1=\"Top\" display_status2=\"7th\" href=\"http://scores.nbcsports.msnbc.com/mlb/boxscore.asp?gamecode=330413124\" tv=\"FSWI/FSMW\" gametime=\"4:15 PM\" gamedate=\"4/13\" is-dst=\"1\" is-world-dst=\"1\" reason=\"\"></gamestate></ticker-entry>"
,"<ticker-entry gamecode=\"330413123\" gametype=\"Regular Season\" league=\"NL\"><visiting-team display_name=\"Cincinnati\" alias=\"Cin\" nickname=\"Reds\" id=\"17\" division=\"NLC\" league=\"NL\" score=\"\"><score heading=\"R\" value=\"\"></score><score heading=\"H\" value=\"\"></score><score heading=\"E\" value=\"\"></score></visiting-team><home-team display_name=\"Pittsburgh\" alias=\"Pit\" nickname=\"Pirates\" id=\"23\" division=\"NLC\" league=\"NL\" score=\"\"><score heading=\"R\" value=\"\"></score><score heading=\"H\" value=\"\"></score><score heading=\"E\" value=\"\"></score></home-team><gamestate status=\"Pre-Game\" display_status1=\"7:05 PM\" display_status2=\"\" href=\"http://scores.nbcsports.msnbc.com/mlb/preview.asp?g=330413123\" tv=\"FSOH/ROOT\" gametime=\"7:05 PM\" gamedate=\"4/13\" is-dst=\"1\" is-world-dst=\"1\" reason=\"\"></gamestate></ticker-entry>"
,"<ticker-entry gamecode=\"330413107\" gametype=\"Regular Season\" league=\"AL\"><visiting-team display_name=\"Toronto\" alias=\"Tor\" nickname=\"Blue Jays\" id=\"14\" division=\"ALE\" league=\"AL\" score=\"\"><score heading=\"R\" value=\"\"></score><score heading=\"H\" value=\"\"></score><score heading=\"E\" value=\"\"></score></visiting-team><home-team display_name=\"Kansas City\" alias=\"KC\" nickname=\"Royals\" id=\"7\" division=\"ALC\" league=\"AL\" score=\"\"><score heading=\"R\" value=\"\"></score><score heading=\"H\" value=\"\"></score><score heading=\"E\" value=\"\"></score></home-team><gamestate status=\"Pre-Game\" display_status1=\"7:10 PM\" display_status2=\"\" href=\"http://scores.nbcsports.msnbc.com/mlb/preview.asp?g=330413107\" tv=\"RSN/FSKC\" gametime=\"7:10 PM\" gamedate=\"4/13\" is-dst=\"1\" is-world-dst=\"1\" reason=\"\"></gamestate></ticker-entry>"
,"<ticker-entry gamecode=\"330413128\" gametype=\"Regular Season\" league=\"NL\"><visiting-team display_name=\"Philadelphia\" alias=\"Phi\" nickname=\"Phillies\" id=\"22\" division=\"NLE\" league=\"NL\" score=\"\"><score heading=\"R\" value=\"\"></score><score heading=\"H\" value=\"\"></score><score heading=\"E\" value=\"\"></score></visiting-team><home-team display_name=\"Miami\" alias=\"Mia\" nickname=\"Marlins\" id=\"28\" division=\"NLE\" league=\"NL\" score=\"\"><score heading=\"R\" value=\"\"></score><score heading=\"H\" value=\"\"></score><score heading=\"E\" value=\"\"></score></home-team><gamestate status=\"Pre-Game\" display_status1=\"7:10 PM\" display_status2=\"\" href=\"http://scores.nbcsports.msnbc.com/mlb/preview.asp?g=330413128\" tv=\"CSN/FSFL\" gametime=\"7:10 PM\" gamedate=\"4/13\" is-dst=\"1\" is-world-dst=\"1\" reason=\"\"></gamestate></ticker-entry>"
,"<ticker-entry gamecode=\"330413129\" gametype=\"Regular Season\" league=\"NL\"><visiting-team display_name=\"LA Dodgers\" alias=\"LAD\" nickname=\"Dodgers\" id=\"19\" division=\"NLW\" league=\"NL\" score=\"\"><score heading=\"R\" value=\"\"></score><score heading=\"H\" value=\"\"></score><score heading=\"E\" value=\"\"></score></visiting-team><home-team display_name=\"Arizona\" alias=\"Ari\" nickname=\"Diamondbacks\" id=\"29\" division=\"NLW\" league=\"NL\" score=\"\"><score heading=\"R\" value=\"\"></score><score heading=\"H\" value=\"\"></score><score heading=\"E\" value=\"\"></score></home-team><gamestate status=\"Pre-Game\" display_status1=\"8:10 PM\" display_status2=\"\" href=\"http://scores.nbcsports.msnbc.com/mlb/preview.asp?g=330413129\" tv=\"KCAL/FSAZ\" gametime=\"8:10 PM\" gamedate=\"4/13\" is-dst=\"1\" is-world-dst=\"1\" reason=\"\"></gamestate></ticker-entry>"
,"<ticker-entry gamecode=\"330413125\" gametype=\"Regular Season\" league=\"NL\"><visiting-team display_name=\"Colorado\" alias=\"Col\" nickname=\"Rockies\" id=\"27\" division=\"NLW\" league=\"NL\" score=\"\"><score heading=\"R\" value=\"\"></score><score heading=\"H\" value=\"\"></score><score heading=\"E\" value=\"\"></score></visiting-team><home-team display_name=\"San Diego\" alias=\"SD\" nickname=\"Padres\" id=\"25\" division=\"NLW\" league=\"NL\" score=\"\"><score heading=\"R\" value=\"\"></score><score heading=\"H\" value=\"\"></score><score heading=\"E\" value=\"\"></score></home-team><gamestate status=\"Pre-Game\" display_status1=\"8:40 PM\" display_status2=\"\" href=\"http://scores.nbcsports.msnbc.com/mlb/preview.asp?g=330413125\" tv=\"ROOT/FSSD\" gametime=\"8:40 PM\" gamedate=\"4/13\" is-dst=\"1\" is-world-dst=\"1\" reason=\"\"></gamestate></ticker-entry>"
,"<ticker-entry gamecode=\"330413103\" gametype=\"Regular Season\" league=\"AL\"><visiting-team display_name=\"Houston\" alias=\"Hou\" nickname=\"Astros\" id=\"18\" division=\"ALW\" league=\"AL\" score=\"\"><score heading=\"R\" value=\"\"></score><score heading=\"H\" value=\"\"></score><score heading=\"E\" value=\"\"></score></visiting-team><home-team display_name=\"LA Angels\" alias=\"LAA\" nickname=\"Angels\" id=\"3\" division=\"ALW\" league=\"AL\" score=\"\"><score heading=\"R\" value=\"\"></score><score heading=\"H\" value=\"\"></score><score heading=\"E\" value=\"\"></score></home-team><gamestate status=\"Pre-Game\" display_status1=\"9:05 PM\" display_status2=\"\" href=\"http://scores.nbcsports.msnbc.com/mlb/preview.asp?g=330413103\" tv=\"CSN/FSW\" gametime=\"9:05 PM\" gamedate=\"4/13\" is-dst=\"1\" is-world-dst=\"1\" reason=\"\"></gamestate></ticker-entry>"
,"<ticker-entry gamecode=\"330413112\" gametype=\"Regular Season\" league=\"AL\"><visiting-team display_name=\"Texas\" alias=\"Tex\" nickname=\"Rangers\" id=\"13\" division=\"ALW\" league=\"AL\" score=\"\"><score heading=\"R\" value=\"\"></score><score heading=\"H\" value=\"\"></score><score heading=\"E\" value=\"\"></score></visiting-team><home-team display_name=\"Seattle\" alias=\"Sea\" nickname=\"Mariners\" id=\"12\" division=\"ALW\" league=\"AL\" score=\"\"><score heading=\"R\" value=\"\"></score><score heading=\"H\" value=\"\"></score><score heading=\"E\" value=\"\"></score></home-team><gamestate status=\"Pre-Game\" display_status1=\"9:10 PM\" display_status2=\"\" href=\"http://scores.nbcsports.msnbc.com/mlb/preview.asp?g=330413112\" tv=\"FSSW/ROOT\" gametime=\"9:10 PM\" gamedate=\"4/13\" is-dst=\"1\" is-world-dst=\"1\" reason=\"\"></gamestate></ticker-entry>"
,"<ticker-entry gamecode=\"330413102\" gametype=\"Regular Season\" league=\"AL\"><visiting-team display_name=\"Tampa Bay\" alias=\"TB\" nickname=\"Rays\" id=\"30\" division=\"ALE\" league=\"AL\" score=\"1\"><score heading=\"1\" value=\"0\"></score><score heading=\"2\" value=\"0\"></score><score heading=\"3\" value=\"1\"></score><score heading=\"4\" value=\"0\"></score><score heading=\"5\" value=\"0\"></score><score heading=\"6\" value=\"0\"></score><score heading=\"7\" value=\"0\"></score><score heading=\"8\" value=\"0\"></score><score heading=\"9\" value=\"0\"></score><score heading=\"10\" value=\"0\"></score><score heading=\"R\" value=\"1\"></score><score heading=\"H\" value=\"6\"></score><score heading=\"E\" value=\"1\"></score></visiting-team><home-team display_name=\"Boston\" alias=\"Bos\" nickname=\"Red Sox\" id=\"2\" division=\"ALE\" league=\"AL\" score=\"2\"><score heading=\"1\" value=\"0\"></score><score heading=\"2\" value=\"0\"></score><score heading=\"3\" value=\"0\"></score><score heading=\"4\" value=\"0\"></score><score heading=\"5\" value=\"1\"></score><score heading=\"6\" value=\"0\"></score><score heading=\"7\" value=\"0\"></score><score heading=\"8\" value=\"0\"></score><score heading=\"9\" value=\"0\"></score><score heading=\"10\" value=\"1\"></score><score heading=\"R\" value=\"2\"></score><score heading=\"H\" value=\"7\"></score><score heading=\"E\" value=\"0\"></score></home-team><gamestate status=\"Final\" display_status1=\"Final\" display_status2=\"10\" href=\"http://scores.nbcsports.msnbc.com/mlb/recap.asp?g=330413102\" tv=\"FOX\" gametime=\"1:05 PM\" gamedate=\"4/13\" is-dst=\"1\" is-world-dst=\"1\" reason=\"\"></gamestate></ticker-entry>"
,"<ticker-entry gamecode=\"330413116\" gametype=\"Regular Season\" league=\"NL\"><visiting-team display_name=\"San Francisco\" alias=\"SF\" nickname=\"Giants\" id=\"26\" division=\"NLW\" league=\"NL\" score=\"3\"><score heading=\"1\" value=\"0\"></score><score heading=\"2\" value=\"0\"></score><score heading=\"3\" value=\"0\"></score><score heading=\"4\" value=\"0\"></score><score heading=\"5\" value=\"1\"></score><score heading=\"6\" value=\"1\"></score><score heading=\"7\" value=\"1\"></score><score heading=\"8\" value=\"0\"></score><score heading=\"9\" value=\"0\"></score><score heading=\"R\" value=\"3\"></score><score heading=\"H\" value=\"9\"></score><score heading=\"E\" value=\"1\"></score></visiting-team><home-team display_name=\"Chi Cubs\" alias=\"ChC\" nickname=\"Cubs\" id=\"16\" division=\"NLC\" league=\"NL\" score=\"2\"><score heading=\"1\" value=\"0\"></score><score heading=\"2\" value=\"0\"></score><score heading=\"3\" value=\"0\"></score><score heading=\"4\" value=\"0\"></score><score heading=\"5\" value=\"0\"></score><score heading=\"6\" value=\"0\"></score><score heading=\"7\" value=\"2\"></score><score heading=\"8\" value=\"0\"></score><score heading=\"9\" value=\"0\"></score><score heading=\"R\" value=\"2\"></score><score heading=\"H\" value=\"7\"></score><score heading=\"E\" value=\"0\"></score></home-team><gamestate status=\"Final\" display_status1=\"Final\" display_status2=\"\" href=\"http://scores.nbcsports.msnbc.com/mlb/recap.asp?g=330413116\" tv=\"FOX\" gametime=\"1:05 PM\" gamedate=\"4/13\" is-dst=\"1\" is-world-dst=\"1\" reason=\"\"></gamestate></ticker-entry>"
,"<ticker-entry gamecode=\"330413120\" gametype=\"Regular Season\" league=\"NL\"><visiting-team display_name=\"Atlanta\" alias=\"Atl\" nickname=\"Braves\" id=\"15\" division=\"NLE\" league=\"NL\" score=\"3\"><score heading=\"1\" value=\"0\"></score><score heading=\"2\" value=\"0\"></score><score heading=\"3\" value=\"2\"></score><score heading=\"4\" value=\"0\"></score><score heading=\"5\" value=\"0\"></score><score heading=\"6\" value=\"0\"></score><score heading=\"7\" value=\"0\"></score><score heading=\"8\" value=\"0\"></score><score heading=\"9\" value=\"1\"></score><score heading=\"R\" value=\"3\"></score><score heading=\"H\" value=\"8\"></score><score heading=\"E\" value=\"1\"></score></visiting-team><home-team display_name=\"Washington\" alias=\"Was\" nickname=\"Nationals\" id=\"20\" division=\"NLE\" league=\"NL\" score=\"1\"><score heading=\"1\" value=\"0\"></score><score heading=\"2\" value=\"0\"></score><score heading=\"3\" value=\"0\"></score><score heading=\"4\" value=\"0\"></score><score heading=\"5\" value=\"1\"></score><score heading=\"6\" value=\"0\"></score><score heading=\"7\" value=\"0\"></score><score heading=\"8\" value=\"0\"></score><score heading=\"9\" value=\"0\"></score><score heading=\"R\" value=\"1\"></score><score heading=\"H\" value=\"4\"></score><score heading=\"E\" value=\"1\"></score></home-team><gamestate status=\"Final\" display_status1=\"Final\" display_status2=\"\" href=\"http://scores.nbcsports.msnbc.com/mlb/recap.asp?g=330413120\" tv=\"FOX\" gametime=\"1:05 PM\" gamedate=\"4/13\" is-dst=\"1\" is-world-dst=\"1\" reason=\"\"></gamestate></ticker-entry>"
]}
);

这是一个包含一堆 XML 的 JSON 记录。您可以使用 PHP 解析 JSON“信封”以及 XML“数据记录”数组。我上面提供的链接应该为您指明正确的方向。

'希望有帮助

【讨论】:

  • 有趣,当我转到链接时,我没有看到 XML。不过,您可以通过从链接中删除 jsonp=true&amp; 来关闭填充。 ^^
  • 当我这样做时,我看不到 XML。我们在这里缺少什么?
【解决方案2】:

就这样吧:

$url = 'http://scores.nbcsports.msnbc.com/ticker/data/gamesMSNBC.js.asp?sport=MLB&period=20130413';

$data = file_get_contents($url);
$records = json_decode($data);

$xmlDocument = implode("\n", $records->games);
$xmlObjects = simplexml_load_string('<?xml version=\'1.0\'?> <document>'. $xmlDocument . '</document>');

foreach($xmlObjects AS $xml){
    // get all attributes
    foreach($xml->attributes() AS $attribute=>$value){
        echo $attribute . ' = '.$value ."\n"; // i.e: gamecode = 330413105
    }

    // visiting team: 
    $visitingTeam='visiting-team';
    print_r($xml->$visitingTeam);

    // get visiting team attributes, like display_name, etc
    foreach($xml->$visitingTeam->attributes() AS $attribute => $value){
        echo $attribute . ' = '.$value ."\n"; 
    }
}

【讨论】:

    【解决方案3】:

    PHP 有:

    json_decode();
    

    用于读取 JSON。括号内的数据是一个 JSON 对象。

    【讨论】:

    • 那不完全是 JSON 对象..再看一遍
    • 真的吗?你不赞成我的回答? loadGamesData() 函数里面的数据确实是 JSON,这正是我所说的。
    【解决方案4】:

    编辑 正如 Diego 指出的那样,不要使用您当前的 url,只需完全删除参数 jsonp=true 并解析原始 JSON。

    看看这是否有帮助。本质上,我们使用正则表达式提取 {} 之间的所有内容,然后将 json_decode() 应用于每个匹配项。

    <?php
    
        $data = 'shsMSNBCTicker.loadGamesData({ "sport": "MLB", "period": "20130413", "games": [ "" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ]} );';
    
        //this will extract everything between and including the {}
        preg_match("/(\{.*\})/", $data, $matches);
    
        echo "<pre>";
        //print everything that matched
        print_r($matches);
    
        //now you can decode it
        $firstMatch = json_decode($matches[0]);
    
        //print out the first match
        print_r($firstMatch);
    
        //now you can do stuff like
    
        echo $firstMatch->sport;
    
    ?>
    

    【讨论】:

    • ...或者您可以从 URL 中删除 jsonp=true 参数,它告诉 Web 服务输出 JSONP 而不是 JSON。
    • 是的,我注意到,但是使用正则表达式,他可以放入纯垃圾,但只要花括号之间的数据完好无损,preg_match 仍然可以提取所有有效的 json 对象。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-07
    • 2012-03-06
    • 2018-05-08
    • 2011-01-05
    • 1970-01-01
    相关资源
    最近更新 更多