2

我正在尝试使用 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来看看会发生什么。在第一种情况下,我只收到浏览器中显示的文本(在我未经训练的眼睛看来,这就像一个空数组)。第二种情况完全失败(“预期开始标签”)。

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

更新

我忘了提到我也试图json_decode在 url 上回显无济于事。什么都没有回响。

4

4 回答 4

4

您的链接包括以下内容:

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“数据记录”数组。我上面提供的链接应该为您指明正确的方向。

'希望有帮助

于 2013-04-13T22:25:59.060 回答
3

Just do it like this:

$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"; 
    }
}
于 2013-04-13T22:37:37.703 回答
1

编辑 正如 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;

?>
于 2013-04-13T22:23:27.513 回答
1

PHP 有:

json_decode();

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

于 2013-04-13T22:11:24.680 回答