-4

I am using following code to get bitcoin price it is working off line but not working on server.

$api = "http://blockchain.info/ticker";
$json = file_get_contents($api);
$data = json_decode($json, TRUE);
$rate = $data["USD"]["15m"];
$symbol = $data["USD"]["symbol"];

echo $rate.$symbol;
?>
Neeraj Kumar
  • 3,851
  • 2
  • 19
  • 41
aniljat
  • 11
  • 3
  • What is happening instead on your server? Is there any error message? – Nico Haase Dec 05 '17 at 12:47
  • no there is no error .., localhost display value but server is not getting value – aniljat Dec 05 '17 at 12:54
  • 1
    Have you tried using https://blockchain.info/ticker, and did you have a look into the servers error log? – Nico Haase Dec 05 '17 at 12:58
  • Sounds like the firewall on the server is blocking the request. – Jay Blanchard Dec 05 '17 at 13:00
  • yer there is an error in error log that is PHP Warning: file_get_contents(https://blockchain.info/ticker): failed to open stream: no suitable wrapper could be found in /home/jatshakt/bitcoinbull.in/home.php on line 31 – aniljat Dec 05 '17 at 13:18
  • So, the main problem is that file_get_contents cannot work with the HTTPS url. See https://stackoverflow.com/questions/1975461/how-to-get-file-get-contents-to-work-with-https for a solution - that will keep your code much shorter than the curl option – Nico Haase Dec 05 '17 at 14:55

2 Answers2

0

Solve when i use curl

$url = "http://blockchain.info/ticker";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
curl_close($ch);
$data = json_decode($result, TRUE);
$rate = $data["USD"]["15m"];
$symbol = $data["USD"]["symbol"];
echo $rate.$symbol;

thanks to all for support..

aniljat
  • 11
  • 3
-1

This method is not secure to perform a request to other server. You should use curl instead of file_get_contents. Btw, if still you want to keep this approach you can enable it in php.ini setting allow_url_fopen to 1

Jose Mato
  • 2,709
  • 1
  • 17
  • 18
  • 1
    What is the difference between file_get_contents and an approach using curl? Both gather data from an external ressource, both work to read data from a URL – Nico Haase Dec 05 '17 at 12:46
  • Usually file_get_contents is used to read files from server, if a security hole appears in the app, an attacker could inject remote code (keeping in mind if the bug exist and file_get_contents is configured to accept http/s calls) – Jose Mato Dec 05 '17 at 14:44
  • Sorry, I don't see where curl helps you in avoiding some mysterious security holes that file_get_contents would leave open. Both just read files, they do not execute remote code, and if I can inject a URL to be opened into the application, both executions will call that URL – Nico Haase Dec 05 '17 at 14:53
  • All depends about what the app is doing in the background. If we want to use file_get_contents we should do it just to read files from our web app without call any remote endpoint, for that, we need to use curl but if a developer is using file_get_contents to execute local php files or whatever then an attacker could inject remote code from url (as I said, always depends on the job the app is doing, and we need to put some problems to the attacker in each phase) – Jose Mato Dec 05 '17 at 15:04
  • The result of file_get_contents is never ever executed, so you can never ever inject remote code. And both endpoints can be forged in the same way if the app contains an error – Nico Haase Dec 05 '17 at 15:06