3

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?

1 Answers1

4

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.

Saeven
  • 2,280
  • 1
  • 20
  • 33
  • Amazing, it worked! Will the request be stored in the variable $result? cause it seems to be a boolean. I would like the requested text from the website in a variable, is that possible? – user3055331 Jan 30 '14 at 22:23
  • If you want the transfer in $result, just add CURLOPT_RETURNTRANSFER – Saeven Jan 30 '14 at 22:26
  • And, glad it helped you out :) – Saeven Jan 30 '14 at 22:27
  • I don't really understand, new word and stuff, never heard of curl.. If I want the requested text from the website in a variable, how could I do that? ELI5 – user3055331 Jan 30 '14 at 22:29
  • Edited, note the mild differences :) Added curl_setopt call for you, which adjusts how the curl handler works, and stuck a var dump so you can see what your server returns. – Saeven Jan 30 '14 at 22:35
  • You are awesome! Now I only have one problem, I want the user to be redirected directly after this call, and I use header('Location:), but it says the header information was already sent by the "var_dump( $response ); " line. Is there a way to work this out? EDIT: I fixed it, just removed the dump ;) Thank you SO much! – user3055331 Jan 30 '14 at 22:45