【问题标题】:Can not use file_get_contents() for certain url from my server (php)不能使用 file_get_contents() 来自我的服务器的某些 url (php)
【发布时间】:2014-01-30 22:10:55
【问题描述】:

所以我正在向一个网址(https:// 地址)发出获取请求,我只想从中接收一小段文本。这在 localhost 上运行良好,但是当我在我的网络服务器(由 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

我可以从网络服务器发出其他的获取请求,但不是这个特定的请求。可能是什么原因?

【问题讨论】:

  • 很可能是一些损坏的 ssl 库?你能在命令行那里wget url吗?
  • file_get_contents() 不应该用于 https 连接使用 curl

标签: php file api get


【解决方案1】:

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

【讨论】:

  • 太棒了,它成功了!请求是否会存储在变量 $result 中?因为它似乎是一个布尔值。我想将网站请求的文本放在一个变量中,这可能吗?
  • 如果你想在 $result 中转账,只需添加 CURLOPT_RETURNTRANSFER
  • 而且,很高兴它帮到了你:)
  • 我不太明白,新词和东西,从来没有听说过 curl.. 如果我想从网站请求的文本放在一个变量中,我该怎么做? ELI5
  • 已编辑,注意细微差别:) 为您添加了 curl_setopt 调用,它调整 curl 处理程序的工作方式,并卡住了一个 var 转储,以便您查看服务器返回的内容。
猜你喜欢
  • 1970-01-01
  • 2020-05-22
  • 2013-06-26
  • 1970-01-01
  • 1970-01-01
  • 2011-03-29
  • 2021-02-16
  • 2020-02-02
相关资源
最近更新 更多