-2

I have been using this code for the past 2-3 months to grab json data variables that are needed. When the url changes to https:// it errors out and won't display. Is there a different way to grab https from http for json url's and php? I can't seem to find anything in terms of a good example or documentation.

<?
$btcjsonurl = "http://blockchain.info/stats?format=json";
$btcjson = file_get_contents($btcjsonurl,0,null,null);
$btcdata = json_decode($btcjson, true);
$btcdiff = $btcdata['difficulty'];
echo $btcdiff;
?>
ajankuv
  • 499
  • 3
  • 22

2 Answers2

1

What's the error?

See this (possible dupe)

Unless, is there any reason why you can't use curl, or a similar php client?

Https requires extra client side implementation for SSL etc, whereas all file_get_contents, called without $opts, will do functionally is read a file stream. For example, if the hostname in the SSL certificate is not the one in the link you pass to file get contents, the implementation of https requires you- the client- to verify this (typically by looking up in a list of allowed host you would need to manually write). But because you're not working directly with a client, you have no opportunity to handle this.

Another route would be to use stream_context_create of course, but that's covered in the link above so I won't repeat it.

Hope this helps.

Community
  • 1
  • 1
Tom
  • 1,773
  • 15
  • 23
0

Most probably, the target URL is on a server whose HTTPS certificate is not signed with a provider your PHP knows. Try navigating your browser to the page, and see if it shows the padlock by the URL. If you get any security warnings then you surely found one thing that prevents PHP from downloading the contents.

Levente Pánczél
  • 1,895
  • 2
  • 14
  • 16