douba7784
douba7784
2016-01-18 07:42

用PHP解析JSONP

已采纳

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?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dongshan3759 dongshan3759 5年前

    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);
    
    }
    
    $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;
    }
    
    点赞 评论 复制链接分享