在 php 数组中解码 "https" json-数组

Decode "https" json-array in a php 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 urls。

你能帮我吗,我怎样才能在 php 中解码这个 json 数组,这样我就可以像“正常”一样继续使用它 php数组?

我也尝试了这个 curl 解决方案但没有成功:

$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, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}

原因是 API 提供商正在阻止 CLI 类型 User-Agents,很可能是为了防止机器人程序和此类事情。

如果您将用户代理设置为使用 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, $url);
    curl_setopt($ch, 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);