在 PHP 中解析 JSONP
Parsing JSONP in PHP
我正在尝试解析 this JSONP file。过去我在解析 JSON 时运气不错,这是我第一次尝试 JSONP。我在该站点上的一些其他线程上读到,您可以使用 substr()
通过剥离 JavaScript 函数调用来获取数据。这是我的尝试:
<?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;
}
?>
到目前为止,我得到了这两个错误:
出于安全原因,警告 file_get_contents() 在第 2
行已被禁用
警告为第 7 行的 foreach() 提供的参数无效
另外,我知道 file_get_contents()
没有被禁用,因为我在下面的项目 *link 的多个其他地方使用了该功能。
这是我当前的输出(对于 JSONP 为空):
空 1453102048 The Null and $date are hidden echo's beneath the stats link
问题基本上是这样的......我做错了什么?
如果您需要可变 url,最好将获取包装到一个函数中。
参见下面的示例
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);
将输出 $json
对象
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] =>
迭代内容
foreach($json->games as $game){
print $game->canationalbroadcasts . PHP_EOL;
}
我正在尝试解析 this JSONP file。过去我在解析 JSON 时运气不错,这是我第一次尝试 JSONP。我在该站点上的一些其他线程上读到,您可以使用 substr()
通过剥离 JavaScript 函数调用来获取数据。这是我的尝试:
<?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;
}
?>
到目前为止,我得到了这两个错误:
出于安全原因,警告 file_get_contents() 在第 2
行已被禁用
警告为第 7 行的 foreach() 提供的参数无效
另外,我知道 file_get_contents()
没有被禁用,因为我在下面的项目 *link 的多个其他地方使用了该功能。
这是我当前的输出(对于 JSONP 为空):
空 1453102048 The Null and $date are hidden echo's beneath the stats link
问题基本上是这样的......我做错了什么?
如果您需要可变 url,最好将获取包装到一个函数中。
参见下面的示例
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);
将输出 $json
对象
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] =>
迭代内容
foreach($json->games as $game){
print $game->canationalbroadcasts . PHP_EOL;
}