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?

2
  • Most likely some broken ssl library? Can you wget the url from the command line there? Commented Jan 30, 2014 at 22:14
  • 1
    file_get_contents() should not be used for https connections use curl Commented Jan 30, 2014 at 22:15

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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?
If you want the transfer in $result, just add CURLOPT_RETURNTRANSFER
And, glad it helped you out :)
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
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.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.