PHP uses Curl access domain name to return 405 method does not allowed hint solution _php skill

Source: Internet
Author: User
Tags curl
/** * HTTP Test * Note: PHP version 5.2 or more to support CURL_IPRESOLVE_V4 * @param $url website domain name * @param $type website Access Protocol * @param $ipresolve Resolution *
     /Public Function Web_http ($url, $type, $ipresolve) {//Set header 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 location jump links until returned 200OK curl_setopt ($ch, curlopt_followlocation, 1);
      Do not output the body part of HTML curl_setopt ($ch, curlopt_nobody, 1);
      Returns the result as a file stream, not directly output curl_setopt ($ch, Curlopt_returntransfer, 1);
      Access to if ($ipresolve = = ' IPv6 ') {curl_setopt ($ch, curlopt_ipresolve,curl_ipresolve_v6) in ipv4/ipv6 manner;
      }else{curl_setopt ($ch, CURLOPT_IPRESOLVE,CURL_IPRESOLVE_V4); }//Add HTTP header header request CU with compression and get methodRl_setopt ($ch, Curlopt_httpheader, $header);
      curl_setopt ($ch, curlopt_encoding, "gzip");
      curl_setopt ($ch, curlopt_customrequest, ' get ');
      Clears the DNS cache curl_setopt ($ch, curlopt_dns_cache_timeout,0);
      Set the connection timeout curl_setopt ($ch, curlopt_connecttimeout,15);
      Set the Access timeout curl_setopt ($ch, curlopt_timeout,50); Set User-agent curl_setopt ($ch, Curlopt_useragent, ' mozilla/5.0 (Windows NT 6.1) applewebkit/536.11 (khtml, like Gecko
      ) chrome/20.0.1132.47 safari/536.11 ');
          if ($type = = "https") {//No inspection curl_setopt ($ch, Curlopt_ssl_verifypeer, false) from the source of authentication certificate;
      Checks whether the SSL encryption algorithm exists curl_setopt ($ch, Curlopt_ssl_verifyhost, True) from the certificate;
      }//Perform curl operation $html = curl_exec ($ch);
      Gets information about a Curl connection resource handle (gets information about the last transmission) $info = Curl_getinfo ($ch);
    Curl_close ($ch);
  return $info; }

The above is a basic curl access method, because here need to use IPv6 way, so add the corresponding options, I believe we can see clearly, usually use the options above all have appeared, we need to choose.

The status code prompts 405/method not allowed to indicate a method that does not support the request, an error that is not common.

This is caused by the fact that the post method does not have permission under this type of domain name because the curl default is to submit access by post . For example, when testing www.amazon.cn, this kind of problem arises. and modified to get the way, and added header head, can be normal access, personal speculation, perhaps the Amazon side is basically using get way, will be considered an artificial click, the post did the corresponding shielding.

The following code was added to this:

Set Header header
$header [] = "Accept:application/json";
$header [] = "Accept-encoding:gzip";
Add HTTP header headers request
curl_setopt ($ch, Curlopt_httpheader, $header) using compression and get mode;
curl_setopt ($ch, curlopt_encoding, "gzip");
curl_setopt ($ch, curlopt_customrequest, ' get ');

The form of the command line 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.