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;
}
Kelser
  • 13
  • 3
  • 1
    I'm completely unable to see any sort of JSON in that URL, you mean that that url returns JSON? – Alberto Sinigaglia Sep 05 '20 at 00:59
  • Also, this might help: https://stackoverflow.com/questions/5522636/get-file-content-from-url/15708596#15708596 – Alberto Sinigaglia Sep 05 '20 at 01:02
  • Does this answer your question? [How to get file\_get\_contents() to work with HTTPS?](https://stackoverflow.com/questions/1975461/how-to-get-file-get-contents-to-work-with-https) – catcon Sep 05 '20 at 01:04
  • I tried [link]https://stackoverflow.com/questions/1975461/how-to-get-file-get-contents-to-work-with-https. But it didn´t worked. @catcon – Kelser Sep 05 '20 at 01:09
  • @Berto I think this returns JSON, isn´t it? – Kelser Sep 05 '20 at 01:11
  • @Kelser Yes, that is JSON. Looks like the URL requires a user agent though. Command line gives me 404. – user3783243 Sep 05 '20 at 01:16
  • I tried the "curl" solutions from other threats with no success. How you guys would handle it? – Kelser Sep 05 '20 at 01:22
  • fwiw be careful posting URL's you've left an access token in there. I'm not sure if that's sensitive data or not but just letting you know. Also please post your CURL solution. CURL can most definitely make such requests. – BugHunterUK Sep 05 '20 at 01:25
  • @BugHunterUK Thank you, but it´s just a game api. I added the curl solution in my question. – Kelser Sep 05 '20 at 01:34

1 Answers1

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

Cyril Graze
  • 3,881
  • 2
  • 21
  • 27