使用PHP CURL和SSL无法获取图像

时间:2015-11-05 21:34:43

标签: php ssl curl encryption ssl-certificate

所以我正在尝试使用PHP进行CURL连接....当我在这里使用命令行时返回的内容

curl -iv https://example.com/image.gif
* Hostname was NOT found in DNS cache
*   Trying ip.ad.dr.es.ss...
* Connected to site.com (ip.ad.dr.es.ss) port 443 (#0)
* TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384
* Server certificate: example.com
* Server certificate: Symantec Class 3 Secure Server CA - G4
* Server certificate: VeriSign Class 3 Public Primary Certification Authority - G5
> GET /image.gif HTTP/1.1
> User-Agent: curl/7.37.1
> Host: example.com
> Accept: */*
> 
< HTTP/1.1 200 OK
HTTP/1.1 200 OK
< Content-Type: image/gif
Content-Type: image/gif
< Last-Modified: Thu, 14 Jul 2011 22:16:46 GMT
Last-Modified: Thu, 14 Jul 2011 22:16:46 GMT
< Accept-Ranges: bytes
Accept-Ranges: bytes
< ETag: "09bd8b77342cc1:0"
ETag: "09bd8b77342cc1:0"
* Server Microsoft-IIS/7.5 is not blacklisted
< Server: Microsoft-IIS/7.5
Server: Microsoft-IIS/7.5
< X-Powered-By: ASP.NET
X-Powered-By: ASP.NET
< X-UA-Compatible: IE=EmulateIE10, requiresActiveX=true
X-UA-Compatible: IE=EmulateIE10, requiresActiveX=true
< Date: Thu, 05 Nov 2015 20:57:22 GMT
Date: Thu, 05 Nov 2015 20:57:22 GMT
< Content-Length: 43
Content-Length: 43
< Set-Cookie: BIGipServerSpace=596747530.20480.0000; path=/
Set-Cookie: BIGipServerSpace=596747530.20480.0000; path=/

< 
* Connection #0 to host example.com left intact

以下是我尝试通过PHP访问它的方法

 $ch = curl_init();
  $url_to_check = 'https://example.com/image.gif';
  curl_setopt( $ch, CURLOPT_URL, $url_to_check );
  curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
  curl_setopt($ch, CURLOPT_PORT, 443);
  curl_exec( $ch );
  $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  echo curl_error($ch);
  echo '||';
  echo $httpcode;
  curl_close( $ch );

然后它最终返回

来自服务器的空回复|| 0

我做错了什么?如何通过php CURL相应地使用SSL获取图像?

1 个答案:

答案 0 :(得分:3)

您的PHP使用什么cURL版本?也许它不支持在cURL 7.34.0中添加的TLS 1.2。

或者您的CA捆绑包可能无法识别您要连接的网站的CA.

尝试添加:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

要在PHP中查看cURL的调试输出,就像在命令行中一样,您可以添加:

curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_STDERR, fopen('php://output', 'w'));

这可能会解决这个问题,并在执行请求后调用var_dump(curl_getinfo($ch));