1

I am trying to parse this JSONP file. I have had some luck with parsing JSON in the past and this is the first time I have tried JSONP. I read on some other threads on this site that you can use substr() to get the data by stripping off the JavaScript function call. This is my attempt:

<?php 
$json = file_get_contents("http://live.nhle.com/GameData/GCScoreboard/2016-01-18.jsonp");
$json = json_decode(substr($json, 15, 1));
echo var_dump($json);
$date = strtotime("now");
echo $date;
foreach ($json as $d) {
    $atCommon = $d['atcommon'];
    $idGame = $d['id'];
    $caNationalBroadcasts = $d['canationalbroadcasts'];
    $ata = $d['ata'];
    $r1 = $d['r1'];
    $atsog = $d['atsog'];
    $bs = $d['bs'];
    $htCommon = $d['htcommon'];
    $atn = $d['atn'];
    $hts = $d['hts'];
    $atc = $d['atc'];
    $htn = $d['htn'];
    $usNationalBroadcasts = $d['usnationalbroadcasts'];
    $gcl = $d['gcl'];
    $hta = $d['hta'];
    $ats = $d['ats'];
    $htc = $d['htc'];
    $htsog = $d['htsog'];
    $bsc = $d['bsc'];
    $gs = $d['gs'];
    $gcl1 = $d['gcl1'];

//  echo $htCommon;
//  echo $atCommon;
}
?>  

So far the I get these two errors:
WARNING file_get_contents() has been disabled for security reasons on line number 2

WARNING Invalid argument supplied for foreach() on line number 7

Also, I know file_get_contents() is not disabled because I am using that function in multiple other places on my project *link below.

Here is my current output (which is null for the JSONP):
NULL 1453102048 The Null and $date are hidden echo's beneath the stats link
The question is basically this...What am I doing wrong?

Kurt Leadley
  • 513
  • 3
  • 20
  • do a `var_dump(ini_get('disable_functions'));` and check the output. If `file_get_contents` is indeed disabled, you can always use `curl` to fetch the file – Alex Andrei Jan 18 '16 at 07:57
  • check this answer on how to properly [parse `jsonp` output into `json`](http://stackoverflow.com/a/5081588/5043552) – Alex Andrei Jan 18 '16 at 08:01
  • @AlexAndrei that answer uses a string in the demo. My goal is to have variables in that link so that instead of having a static link to a certain day, it will update daily. Unless I am missing something obvious, I won't be able to do that with what his demo is showing. – Kurt Leadley Jan 18 '16 at 08:06
  • @AlexAndrei If I have the url be the variable will it work? – Kurt Leadley Jan 18 '16 at 08:09
  • yep, see the answer below :) – Alex Andrei Jan 18 '16 at 08:25
  • Possible duplicate of [PHP: Handling 'JSONP' output vs 'JSON', and its parsing?](http://stackoverflow.com/questions/17613140/php-handling-jsonp-output-vs-json-and-its-parsing) – Martin Tournoij Nov 27 '16 at 06:42

1 Answers1

3

If you need variable urls, it's a good idea to wrap the fetching into a function.

See the example below

function get_JSONP($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    curl_close($ch);
    
    return $output;
    
}

function jsonp_decode($jsonp, $assoc = false) { // PHP 5.3 adds depth as third parameter to json_decode
    if($jsonp[0] !== '[' && $jsonp[0] !== '{') { 
        $jsonp = substr($jsonp, strpos($jsonp, '('));
    }
    $jsonp = trim($jsonp);      // remove trailing newlines
    $jsonp = trim($jsonp,'()'); // remove leading and trailing parenthesis
    
    return json_decode($jsonp, $assoc);
    
}

$url = "http://live.nhle.com/GameData/GCScoreboard/2016-01-18.jsonp";

$jsonp = get_JSONP($url);
$json = jsonp_decode($jsonp);

print_r($json);

Will output the $json object

stdClass Object
(
    [games] => Array
        (
            [0] => stdClass Object
                (
                    [usnationalbroadcasts] =>
                    [atcommon] => OILERS
                    [canationalbroadcasts] =>
                    [rl] =>
                    [ata] => EDM
                    [gcl] => 1
                    [ats] =>
                    [hta] => FLA
                    [htc] =>
                    [bs] => 7:30 PM
                    [htcommon] => PANTHERS
                    [id] => 2015020677
                    [atn] => EDMONTON
                    [hts] =>
                    [atc] =>
                    [gs] => 1
                    [bsc] =>
                    [htn] => FLORIDA
                    [gcll] => 1
                )

            [1] => stdClass Object
                (
                    [usnationalbroadcasts] => NBCSN
                    [atcommon] => PENGUINS
                    [canationalbroadcasts] => TVAS, SNE, SNO, SNP
                    [rl] =>

To iterate over the contents

foreach($json->games as $game){
    print $game->canationalbroadcasts . PHP_EOL;
}
Alex Andrei
  • 7,315
  • 3
  • 28
  • 42