Solution to 403 error in PHPCurl _ PHP Tutorial

Source: Internet
Author: User
Tags idn
Solution for PHPCurl 403 error. You can use a small PHP application to capture and process web pages using curl. for the convenience of wall penetration, Privoxy is used as a proxy to select which websites use proxy and which do not need to be used. However, today I met my own small PHP application. I used curl to capture the web page and process it. in order to make it easier to penetrate the wall, I used Privoxy as a proxy to select which websites use proxy and which do not need to be used. However, today, we have encountered a strange problem. when visiting google baidu, all these websites actually return the 403 error, but accessing other websites is okay. if it is set to not use proxy, the website can be accessed normally.

Does google baidu prevent proxy connection? Obviously, it is not possible. therefore, open the curl information output (curl_setopt ($ this-> mSh, CURLOPT_VERBOSE, 1);) and see the following result:

The code is 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, then you know why this happens ,-)
<
* The requested URL returned error: 403
* Canceled Ed HTTP code 403 from proxy after CONNECT
* Closing connection #0
... Failed.


We can see that the proxy server is working normally. it is true that baidu has returned the 403 error, but the reason is definitely on my side. Finally, I got some inspiration from the Internet (1of2, 2of2)-I used proxytunnel instead of proxy.

In the code, there is such a sentence:

The code is as follows:


Curl_setopt ($ this-> mSh, CURLOPT_HTTPPROXYTUNNEL, true );
Curl_setopt ($ this-> mSh, CURLOPT_PROXY, $ phost );


Php documentation does not provide detailed description, but man curl has a detailed explanation. Both are proxies. proxytunnel (-p parameter) allows other protocols to be transmitted through http proxy, while proxy (-x parameter) only http protocol is supported. So I guess that the google baidu server and the curl proxytunnel are inconsistent, so 403 is returned.

After the first sentence of the above two lines of code is disabled, the curl access returns to normal.

It is strange that there are several different operating systems. for a mac osx, you must explicitly disable proxytunnel. the curl version:

The 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


The other ubuntu is completely unaffected and can be used in any way. curl version:

The 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 the MT host is also fine, curl version:

The 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 seems that it is not entirely about the curl version. mac osx is indeed different.

Another reason is that curl returns the 403 error:

The code is as follows:


Curl_setopt ($ ch, CURLOPT_NOBODY, true );


You need to follow the settings:

The code is as follows:


Curl_setopt ($ ch, CURLOPT_CUSTOMREQUEST, 'Get ');


Otherwise, the 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 ). This is the reason why curl is special on mac osx.

Bytes. 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.