Can not use file_get_contents() for certain url from my server (php)

api, file, get, php

Solution

Best fix is to use cURL

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSLVERSION, 3); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);

var_dump( $result ); // will contain raw return

Important line is the SSLVERSION. Your mileage may vary on the construction of the curl call, but I ran into this before and moving from file_get_contents or any PHP stream to cURL was the solution.

Problem

So I am making a get request to a url, a https:// address, from which I want to receive only a small text. This works fine on localhost, but when I do it on my web server (hosted by one.com) it doesn't work. It shows this error: ``` Warning: file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages: error:14077458:SSL routines:SSL23_GET_SERVER_HELLO:reason(1112) in /customers/0/4/f/mydomain.se/httpd.www/test.php on line 4 Warning: file_get_contents(): Failed to enable crypto in /customers/0/4/f/mydomain.se/httpd.www/test.php on line 4 Warning: file_get_contents(https://dogeapi.com/wow/? api_key={my apikey}&a=get_new_address&address_label=46): failed to open stream: operation failed in /customers/0/4/f/mydomain.se/httpd.www/test.php on line 4 ``` I can make other get requests from the web server, but not this specific one. What could be the cause?

Original source