不能使用file_get_contents()从我的服务器(PHP)的某些网址

时间:2014-01-30 22:10:55

标签: php file api get

所以我正在向一个url(一个https://地址)发出get请求,我只想从中接收一个小文本。这在localhost上工作正常,但是当我在我的Web服务器(由one.com托管)上执行它时,它不起作用。它显示了这个错误:

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

我可以从Web服务器发出其他请求,但不是特定的请求。可能是什么原因?

1 个答案:

答案 0 :(得分:4)

最好的解决方法是使用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

重要的是SSLVERSION。你的里程可能因curl调用的结构而有所不同,但我之前遇到过这种情况,从file_get_contents或任何PHP流转移到cURL是解决方案。