php uses curl to access domain name and return 405 method not allowed prompt

  • 2021-07-04 21:39:48
  • OfStack


/**
 * http Test 
 *  Note: PHP Version 5.2 The above is supported CURL_IPRESOLVE_V4
 * @param $url  Website domain name 
 * @param $type  Website access protocol 
 * @param $ipresolve  Analytic mode 
 */
public function web_http($url,$type,$ipresolve) {
    // Settings Header Head 
    $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 location Jump the link until it returns 200OK
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
      // Wrong HTML In BODY Partially output 
      curl_setopt($ch, CURLOPT_NOBODY, 1);
      // Returns the result as a file stream, not as a direct output 
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      // With IPv4/IPv6 Access in the way of 
      if($ipresolve=='ipv6') {
        curl_setopt($ch,CURLOPT_IPRESOLVE,CURL_IPRESOLVE_V6);
      }else{
        curl_setopt($ch,CURLOPT_IPRESOLVE,CURL_IPRESOLVE_V4);
      }
      // Add HTTP header The head adopts compression and GET Mode request 
      curl_setopt( $ch, CURLOPT_HTTPHEADER, $header );
      curl_setopt($ch,CURLOPT_ENCODING , "gzip");
      curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
      // Clear DNS Cache 
      curl_setopt($ch,CURLOPT_DNS_CACHE_TIMEOUT,0);
      // Set the connection timeout 
      curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,15);
      // Set access timeout 
      curl_setopt($ch,CURLOPT_TIMEOUT,50);
      // Settings 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") {
          // Do not check the source of certification 
          curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
          // Check from the certificate SSL Does an encryption algorithm exist  
          curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);
      }
      // Execute Curl Operation 
      $html = curl_exec($ch);
      // Get 1 A cURL Information about the connection resource handle (get the last 1 Information related to secondary transmission) 
      $info = curl_getinfo($ch);
      curl_close($ch);
    }
    return $info;
  }

The above is a basic curl access method. Because IPv6 needs to be used here, corresponding options have been added. I believe you can see clearly that the options that are often used at ordinary times appear above, so you can choose according to your needs.

The status code prompt 405/Method Not Allowed indicates that the requested method is not supported. This error is not common.

This error occurs because curl defaults to post for committed access, post mode has no authority under such domain names. For example, this kind of problem occurred when testing www. amazon. cn, but it can be accessed normally after being modified to get mode and adding header header. Personally, it is speculated that Amazon basically adopts get mode, which will be considered as artificial click and shielded post accordingly.

The following code has been added to this:


// Settings Header Head 
$header[] = "Accept: application/json";
$header[] = "Accept-Encoding: gzip";
// Add HTTP header The head adopts compression and GET Mode request 
curl_setopt( $ch, CURLOPT_HTTPHEADER, $header );
curl_setopt($ch,CURLOPT_ENCODING , "gzip");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');

The command line takes the form of:


curl -v www.amazon.cn

Related articles: