douba7784 2016-01-18 07:42
浏览 105
已采纳

用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 2016-01-18 08:22
    关注

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题