无法使用 php CURL 和 SSL 获取图像
can't get image using php CURL with SSL
所以我正在尝试使用 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 获取图像?
您 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));
。
所以我正在尝试使用 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 获取图像?
您 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));
。