简体   繁体   中英

Decode “https” json-array in a php array

I have the following https:// url which contains a json array´

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

I want to decode this json array in a php array.

For that I´ve used

$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);

I tried a lot of solutions since it seems to be known that file_get_contents can´t read out https urls.

Could you help me, how can I decode this json array in a php one so I can continue to work with it like it would be a "normal" php array?

I also tried this curl solution with no success:

$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;
}

The reason is that the API provider is blocking CLI type User-Agents, most likely to prevent bots and things of this sort.

If you set the User Agent to emulate an allowed browser one with cURL, it works perfectly:

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);

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM