无法使用带有SSL的php CURL获取图像 [英] can't get image using php CURL with SSL

查看:113
本文介绍了无法使用带有SSL的php CURL获取图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在尝试使用PHP建立CURL连接。...当我使用命令行时,这是返回的内容

  curl -iv https://example.com/image.gif 
*在DNS缓存
中找不到主机名*正在尝试ip.ad.dr.es.ss ...
*连接到site.com(ip.ad.dr.es.ss)端口443(#0)
*使用TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384
的TLS 1.2连接*服务器证书:example.com
*服务器证书:Symantec 3类安全服务器CA-G4
*服务器证书:VeriSign 3类公共主要证书颁发机构-G5
> GET /image.gif HTTP / 1.1
>用户代理:curl / 7.37.1
>主持人:example.com
>接受:* / *
>
< HTTP / 1.1 200好
HTTP / 1.1 200好
<内容类型:图片/ gif
内容类型:图片/ gif
<最后修改时间:2011年7月14日,星期四22:16:46 GMT
最后修改时间:2011年7月14日,星期四22:16:46 GMT
<接受范围:字节
接受范围:字节
< ETag: 09bd8b77342cc1:0
ETag: 09bd8b77342cc1:0
*服务器Microsoft-IIS / 7.5未列入黑名单
<服务器:Microsoft-IIS / 7.5
服务器:Microsoft-IIS / 7.5
< X-Powered-By:ASP.NET
X-Powered-By:ASP.NET
< X-UA相容:IE = EmulateIE10,requiresActiveX = true
X-UA相容:IE = EmulateIE10,requiredActiveX = true
<日期:2015年11月5日星期四20:57:22 GMT
日期:2015年11月5日星期四20:57:22 GMT
<内容长度:43
内容长度:43
<设置cookie:BIGipServerSpace = 596747530.20480.0000; path = /
Set-Cookie:BIGipServerSpace = 596747530.20480.0000; path = /

<
*与主机example.com的连接#0保持不变

这是我尝试的方法通过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。 / p>

尝试添加:

  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)); 执行请求后。


so I'm trying to make a CURL connection using PHP....when I use command line here's what gets returned

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

And here's how I try to access it via 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 );

But then it ends up returning

Empty reply from server||0

What did I do wrong? How can I fetch the image using SSL via php CURL accordingly?

解决方案

What cURL version is your PHP using? Perhaps it doesn't support TLS 1.2 which was added in cURL 7.34.0.

Or your CA bundle might not recognize the CA of the site you are connecting to.

Try adding:

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

To see the debug output from cURL in PHP like you get on the command line, you can add:

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

That might shed some light on the issue, as well as calling var_dump(curl_getinfo($ch)); after the request is executed.

这篇关于无法使用带有SSL的php CURL获取图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆