2

I am attempting to use PHP (which I am pretty new to) to read some data from an API. I am not very familiar with JSON, and have no XML experience. So a good start, then.

The resource in question is:

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

Looking at the source of this page, it seems like it is fairly structured. I attempted to use both file_get_contents and simplexml_load_file on this url to see what would happen. In the first case, I receive only the text that is displayed in the browser (kind of looks like an empty array to my untrained eyes). The second case simply fails ('start tag expected').

What kind of format is this data in, and how should I be trying to access it? How can I pull down the data and, for example, pull out values for certain tags?

Update

I forgot to mention that I also tried to echo json_decode on the url to no avail. Nothing at all was echoed.

6
  • 1
    JSON and XML are, of course, two different things. You might be using either JSON or XML from Ajax (yet a third thing). PHP has great libraries for both JSON and XML: PHP/JSON, PHP/XML and 15-Minute PHP with XML Start. Unfortunately, the developerworks link is down for site maintenance at the moment :( Commented Apr 13, 2013 at 22:11
  • Yeah, I attempted to treat it as each but with no success. I can't even tell from looking what I am dealing with. Commented Apr 13, 2013 at 22:12
  • Thank you, that's helpful. But I'm not sure how to pull down the full contents so that I might then attempt to extract the JSON. Commented Apr 13, 2013 at 22:18
  • 1
    One other good PHP/XML tutorial: blog.teamtreehouse.com/how-to-parse-xml-with-php5. PS: XML always looks like this:<some_tag>something</some_tag>; JSON usually looks more like this: { some_item : "moose"; } A good link on JSON: JSON Tutorial. Commented Apr 13, 2013 at 22:19
  • 2
    What you actually have is JSONP, which is padded JSON and meant to easily be used within <script> tags in a HTML document. Commented Apr 13, 2013 at 22:27

4 Answers 4

4

Your link consists of this:

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>"
]}
);

It's a JSON record that contains a bunch of XML. You can parse both the JSON "envelope", as well as the array of XML "data records", with PHP. The links I gave above should point you in the right direction.

Sign up to request clarification or add additional context in comments.

2 Comments

Interesting, I don't see the XML when I go to the link. Though, you can turn off the padding by removing jsonp=true& from the link. ^^
When I do it I don't see the XML. What are we missing here?
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"; 
    }
}

Comments

1

PHP has:

json_decode();

for reading JSON. That data inside the parens is a JSON object.

2 Comments

thats not exactly a JSON object..look again
Really? You downvote my answer? The data INSIDE the loadGamesData() function is indeed JSON, which is exactly what I said.
1

EDIT As Diego pointed out, dont use your current url, just remove the parameter jsonp=true entirely and just parse the raw JSON.

See if this helps. Essentially we are extracting everything between {} using a regex and then applying json_decode() to each match.

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

?>

2 Comments

...or you could just remove the jsonp=true parameter from the URL, which is what tells the web service to output JSONP instead of JSON.
yepp i noticed that but with the regex, he can put in pure garbage but as long as the data between the curly braces are intact, the preg_match can still pull out all valid json objects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.