PHP Curl 403 Error Resolution _php tutorial

Source: Internet
Author: User
Tags idn ldap
Their own small PHP application, using Curl to grasp the web down processing, in order to wear the wall convenient, using Privoxy as a proxy, easy to choose which sites use proxy, which do not. But today, there are strange problems, visit Google Baidu these sites actually return 403 error, and access to some other sites is OK, if set to not use proxy will be able to access the normal.

Google Baidu will not let you connect with proxy? Obviously impossible, so turn on the information output of curl (curl_setopt ($this->msh, curlopt_verbose, 1);) Look, get the following result:
Copy the Code code as follows:
* Trying 127.0.0.1 ... * Connected
* Connected to 127.0.0.1 (127.0.0.1) port 8118 (#0)
* Establish HTTP proxy tunnel to www.baidu.com:80
> CONNECT www.baidu.com:80 http/1.0
Host:www.baidu.com:80
user-agent:mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)
Proxy-connection:keep-alive

< http/1.0 403 Connection not allowable
< x-hint:if You read this message interactively and then you know why this happens,-)
<
* The requested URL returned error:403
* Received HTTP Code 403 from proxy after CONNECT
* Closing Connection #0
... Failed.
You can see the proxy server is working properly, it is true that Baidu returned 403 errors, but the reason is certainly still on my side. Finally, from the Internet (1OF2, 2OF2) get a little inspiration-I use Proxytunnel instead of proxy.

In the code, there is this sentence:
Copy the Code code as follows:
curl_setopt ($this->msh, Curlopt_httpproxytunnel, true);
curl_setopt ($this->msh, Curlopt_proxy, $phost);
There is no detail in the PHP documentation, but there is a detailed explanation in man curl, both proxies, and the Proxytunnel (-p parameter) allows other protocols to be transmitted over HTTP proxies, while the proxy (-x parameter) can only go through the HTTP protocol. So I guess, Google Baidu's server and Curl Proxytunnel, so return 403.

After disabling the first sentence of the above 2 lines of code, curl access is back to normal.

It is strange that several operating systems are not the same, a Mac OSX will explicitly disable Proxytunnel, curl version:
Copy CodeThe code is as follows:
$ Curl--version
Curl 7.16.3 (powerpc-apple-darwin9.0) libcurl/7.16.3 openssl/0.9.7l zlib/1.2.3
Protocols:tftp ftp telnet dict ldap http file https FTPs
Features:gss-negotiate IPv6 largefile NTLM SSL libz
And another Ubuntu is completely unaffected, how can be used, curl version:
Copy CodeThe code is as follows:
$ Curl--version
Curl 7.18.2 (I486-PC-LINUX-GNU) libcurl/7.18.2 openssl/0.9.8g zlib/1.2.3.3 libidn/1.10
Protocols:tftp ftp telnet dict ldap ldaps http file https FTPs
Features:gss-negotiate IDN IPv6 largefile NTLM SSL libz
CentOS on Mt host is OK, curl version:
Copy CodeThe code is as follows:
$ Curl--version
Curl 7.15.5 (I686-REDHAT-LINUX-GNU) libcurl/7.15.5 openssl/0.9.8b zlib/1.2.3 libidn/0.6.5
Protocols:tftp ftp telnet dict ldap http file https FTPs
Features:gss-negotiate IDN IPv6 largefile NTLM SSL libz
It doesn't seem to be a completely curl version, MAC OSX is a really different thing.

There is also a reason why curl returns a 403 error if it is set:
Copy the Code code as follows:
curl_setopt ($ch, Curlopt_nobody, true);
You need to follow the settings:
Copy the Code code as follows:
curl_setopt ($ch, Curlopt_customrequest, ' GET ');
Otherwise, a 403 error will be returned because the HTTP server does not allow the HEAD command. Reference: Trouble with a CURL request in PHP (http://forums.devshed.com/php-development-5/ trouble-with-a-curl-request-in-php-445222.html). The reason why curl on MAC OSX is special is not ruled out.

http://www.bkjia.com/PHPjc/778128.html www.bkjia.com true http://www.bkjia.com/PHPjc/778128.html techarticle their own small PHP application, using Curl to grasp the web down processing, in order to wear the wall convenient, using Privoxy as a proxy, easy to choose which sites use proxy, which do not. But today, I met ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.