Php uses curl to access the domain name and returns the 405methodnotallowed prompt solution

Source: Internet
Author: User
This article mainly introduces how php uses curl to access the domain name and returns the 405methodnotallowed prompt. For more information, see
/*** Http Test * Note: PHP versions later than 5.2 support CURL_IPRESOLVE_V4 * @ param $ url website domain name * @ param $ type website access protocol * @ param $ ipresolve resolution method */public function web_http ($ url, $ type, $ ipresolve) {// Set the Header $ header [] = "Accept: application/json"; $ header [] = "Accept-Encoding: gzip "; $ httptype = function_exists ('curl _ init '); if (! $ Httptype) {$ html = file_get_contents ($ url);} else {$ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ url ); // output header information curl_setopt ($ ch, CURLOPT_HEADER, 1); // recursive access to the location jump link until 200OK curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 1) is returned ); // do not output curl_setopt ($ ch, CURLOPT_NOBODY, 1) for the BODY part in HTML; // return the result as a file stream instead of directly outputting curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); // access if ($ ipresolve = 'ipv6 ') {curl_setopt ($ ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6) in IPv4/IPv6 mode );} else {curl_setopt ($ ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);} // add an HTTP header to request curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ header) through compression and GET ); curl_setopt ($ ch, CURLOPT_ENCODING, "gzip"); curl_setopt ($ ch, CURLOPT_CUSTOMREQUEST, 'Get'); // clear the DNS cache curl_setopt ($ ch, expires, 0 ); // Set the connection timeout value curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, 15); // Set the access timeout value curl_setopt ($ ch, CURLOPT_TIMEOUT, 50 ); // Set the User-agent curl_setopt ($ ch, CURLOPT_USERAGENT, 'mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/listen 0.1132.47 Safari/100 '); if ($ type = "https") {// do not check the certificate source curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, false ); // check from the certificate whether the SSL encryption algorithm has curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, true);} // perform the Curl operation $ html = curl_exec ($ ch ); // obtain the information of a cURL connection resource handle (obtain the last transmitted information) $ info = curl_getinfo ($ ch); curl_close ($ ch);} return $ info ;}

The above is a basic curl access method. because IPv6 needs to be used here, the corresponding options are added. I believe you can understand it, the frequently used options appear above, and you can choose between them as needed.

Status Code 405/Method Not Allowed indicates that the request Method is Not supported. this error is Not common.

This error is causedBecause curl uses the post method by default for access submission, the post method has no permission under such domain names.For example, this type of problem occurs during the test of www.amazon.cn, and is changed to The get method. after the header is added, the access can be normal. I guess, it may be that Amazon basically uses the get method to be considered as a human click and blocks post.

The following code is added:

// Set the Header $ header [] = "Accept: application/json"; $ header [] = "Accept-Encoding: gzip "; // add an HTTP header to request curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ header) using compression and GET; curl_setopt ($ ch, CURLOPT_ENCODING, "gzip"); curl_setopt ($ ch, CURLOPT_CUSTOMREQUEST, 'Get ');

The command line format is:

curl -v www.amazon.cn

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.