PHP Curl出现403错误怎么办?curl错误解决方法

WBOY
Freigeben: 2016-07-25 08:53:17
Original
2699 Leute haben es durchsucht
  1. curl_setopt($this->mSh, CURLOPT_HTTPPROXYTUNNEL, true);
  2. curl_setopt($this->mSh, CURLOPT_PROXY, $phost);
复制代码

php文档中没有详细说明,不过man curl中有详细解释,两者都是代理,proxytunnel(-p参数)允许其他协议通过http代理传输,而proxy(-x参数)则只能走http协议。所以我猜测,google baidu的服务器和curl的proxytunnel不和,所以返回403。 禁用掉上面2行代码的第一句后,curl访问恢复正常。

另外,几种操作系统下还不一样,一台MAC OSX就要显式的禁用proxytunnel才可以,curl版本:

  1. $ curl --version
  2. curl 7.16.3 (powerpc-apple-darwin9.0) libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3
  3. Protocols: tftp ftp telnet dict ldap http file https ftps
  4. Features: GSS-Negotiate IPv6 Largefile NTLM SSL libz
复制代码

而另外一台ubuntu则完全不受影响,怎么都能用,curl版本:

  1. $ curl --version
  2. curl 7.18.2 (i486-pc-linux-gnu) libcurl/7.18.2 OpenSSL/0.9.8g zlib/1.2.3.3 libidn/1.10
  3. Protocols: tftp ftp telnet dict ldap ldaps http file https ftps
  4. Features: GSS-Negotiate IDN IPv6 Largefile NTLM SSL libz
复制代码

MT主机上的centos也没事,curl版本:

  1. $ curl --version
  2. curl 7.15.5 (i686-redhat-linux-gnu) libcurl/7.15.5 OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.5
  3. Protocols: tftp ftp telnet dict ldap http file https ftps
  4. Features: GSS-Negotiate IDN IPv6 Largefile NTLM SSL libz
复制代码

看来不完全是curl版本问题,MAC OSX的确与众不同啊。 还有一个原因也会导致curl返回403错误,如果设置了:

  1. curl_setopt($ch, CURLOPT_NOBODY, true);
复制代码

则需要紧跟着设置:

  1. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
复制代码

不然会因为http服务器不允许 HEAD 命令而返回403错误。

参考:Trouble with a cURL request in PHP(http://forums.devshed.com/php-development-5/trouble-with-a-curl-request-in-php-445222.html)。 MAC OSX上curl之所以特殊,也不排除是这种原因吧。



Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!