在php数组中解码“ https” json-array

问题描述

我有以下 https:// url ,其中包含一个json数组。

https://us.api.blizzard.com/data/wow/mount/6?namespace=static-us&locale=en_US&access_token=USgdNPkRDwpJ1m4zQ2mHokA0O12E0kPTih

我想在php数组中解码此json数组。

我使用过的

$jsondata = file_get_contents('https://us.api.blizzard.com/data/wow/mount/6?namespace=static-us&locale=en_US&access_token=USgdNPkRDwpJ1m4zQ2mHokA0O12E0kPTih');

$data = json_decode($jsondata,true);

我尝试了许多解决方案,因为似乎file_get_contents无法读取https网址。

您能帮我吗,我该如何在php中解码此json数组,这样我才能像使用“正常”的php数组一样继续使用它?

我也尝试了这种卷曲解决方案,但没有成功:

$url="https://us.api.blizzard.com/data/wow/achievement/6?namespace=static-us&locale=en_US&access_token=USgdNPkRDwpJ1m4zQ2mHokA0O12E0kPTih";    
  
function getSslPage($url) {
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
    curl_setopt($ch,CURLOPT_HEADER,false);
    curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_REFERER,CURLOPT_RETURNTRANSFER,TRUE);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}

解决方法

原因是API提供程序阻止了CLI类型的User-Agent,最有可能阻止机器人和此类活动。

如果您将用户代理设置为使用cURL模拟允许的浏览器,则它会完美运行:

function getSslPage($url,$userAgent) 
{
    $ch = curl_init($url);

    curl_setopt($ch,CURLOPT_USERAGENT,$userAgent);
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
    curl_setopt($ch,CURLOPT_HEADER,false);
    curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_REFERER,CURLOPT_RETURNTRANSFER,TRUE);

    $result = curl_exec($ch);
    curl_close($ch);

    return json_decode($result,true);
}

$userAgent = 'Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0';
 
$url = "https://us.api.blizzard.com/data/wow/achievement/6?namespace=static-us&locale=en_US&access_token=USgdNPkRDwpJ1m4zQ2mHokA0O12E0kPTih";    


$data = getSslPage($url,$userAgent);

print_r($data);

enter image description here

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...