【问题标题】:Using HTTPS link with php methods (file_get_contents, getimagesize)使用带有 php 方法的 HTTPS 链接(file_get_contents、getimagesize)
【发布时间】:2011-12-07 11:33:31
【问题描述】:

当我尝试在我的网站中读取一些 HTTPS 网址时遇到问题。

如果我使用“http”,则没有问题(使用 file_get_contents 和 curl),但是当我将“http”替换为“https”时,这些方法不起作用。

我收到一些错误:

failed to open stream: operation failed occured

Failed to enable crypto occured

SSL operation failed with code 1. OpenSSL Error messages: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol

在我的浏览器中,所有方法都有效: https://ssl10.ovh.net/~fyprbqhq/_perso/facebook.myclimb/test.php(显示屏应显示“OK”)

在 phpinfo() 中我得到了:

openssl
OpenSSL support enabled
OpenSSL Version OpenSSL 0.9.8c 05 Sep 2006

如果你有任何想法。

感谢您的帮助。

(Ps:在我的情况下,get_headers() 不适用于 https)

更多信息:

file_get_contents:

$data = file_get_contents("https://ssl10.ovh.net/~fyprbqhq/_perso/facebook.myclimb/test.php");

卷曲:

$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, "http://ssl10.ovh.net/~fyprbqhq/_perso/facebook.myclimb/test.php");
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT,2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec($curl_handle);
curl_close($curl_handle);

【问题讨论】:

  • 您确定 file_get_contents 可以通过 http 运行吗?我对 fopen/curl 有两个不同的想法,但只有当 file_get_contents 在这两种情况下都失败时它们才是正确的:) 还有.. 你尝试过另一个 https url 吗?
  • 你可以把你用来下载文件内容的那一行放上去。
  • 根据我在其他论坛上阅读的内容,是的 file_get_contents 应该可以工作。但在这些情况下(https),我读到 curl 更合适。尽管如此,我试图阅读一个谷歌 https 链接,它的工作原理(两种方法)。

标签: php https


【解决方案1】:

从您收到的错误(SSL23_GET_SERVER_HELLO:unknown protocol)来看,这几乎可以肯定是由于服务器的 SSL 版本比您的客户端更新。

服务器可能使用 >= 1.0.0 的版本,而您使用的是 0.9.8c

您的 SSL 版本是 2006 年开始的。查看过去 5 年的 list of vulnerabilities in OpenSSL,作为您升级的理由。

很多人都有reported similar experiences .还有herehere

【讨论】:

    【解决方案2】:

    如果您使用的是 PHP 5.6.x,则有一些影响 SSL/TLS 协商的更改。

    在 PHP 5.6 中,所有流包装器现在在使用 SSL/TLS 时默认验证对等证书和主机名。

    要解决没有验证 ssl / tls 套接字的主机,请尝试使用它。

    $ctx = stream_context_create(['ssl' => [
        'capture_session_meta' => TRUE,
        'verify_peer' => false,
        'verify_peer_name' => false
    ]]);
    
    $html = file_get_contents('https://google.com/', FALSE, $ctx);
    $meta = stream_context_get_options($ctx)['ssl']['session_meta'];
    var_dump($meta);
    

    当然不建议这样做,因为您会放弃对主机的验证。

    对我来说,这在访问 flickr api -- api.flickr.com 时发挥了作用。

    参考:http://php.net/manual/en/migration56.openssl.php

    【讨论】:

      猜你喜欢
      • 2018-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-09
      相关资源
      最近更新 更多