1

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

1 Answer 1

1

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

enter image description here

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.