Home Backend Development PHP Tutorial php采用curl访问域名返回405 method not allowed提示的解决方法_php技巧

php采用curl访问域名返回405 method not allowed提示的解决方法_php技巧

May 17, 2016 am 08:40 AM
curl Solution

/**
 * http测试
 * 注:PHP版本5.2以上才支持CURL_IPRESOLVE_V4
 * @param $url 网站域名
 * @param $type 网站访问协议
 * @param $ipresolve 解析方式
 */
public function web_http($url,$type,$ipresolve) {
    //设置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);
      //输出头信息
      curl_setopt($ch, CURLOPT_HEADER, 1);
      //递归访问location跳转的链接,直到返回200OK
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
      //不对HTML中的BODY部分进行输出
      curl_setopt($ch, CURLOPT_NOBODY, 1);
      //将结果以文件流的方式返回,不是直接输出
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      //以IPv4/IPv6的方式访问
      if($ipresolve=='ipv6') {
        curl_setopt($ch,CURLOPT_IPRESOLVE,CURL_IPRESOLVE_V6);
      }else{
        curl_setopt($ch,CURLOPT_IPRESOLVE,CURL_IPRESOLVE_V4);
      }
      //添加HTTP header头采用压缩和GET方式请求
      curl_setopt( $ch, CURLOPT_HTTPHEADER, $header );
      curl_setopt($ch,CURLOPT_ENCODING , "gzip");
      curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
      //清除DNS缓存
      curl_setopt($ch,CURLOPT_DNS_CACHE_TIMEOUT,0);
      //设置连接超时时间
      curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,15);
      //设置访问超时
      curl_setopt($ch,CURLOPT_TIMEOUT,50);
      //设置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") {
          //不对认证证书来源的检查
          curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
          //从证书中检查SSL加密算法是否存在 
          curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);
      }
      //执行Curl操作
      $html = curl_exec($ch);
      //获取一个cURL连接资源句柄的信息(获取最后一次传输的相关信息)
      $info = curl_getinfo($ch);
      curl_close($ch);
    }
    return $info;
  }
Copy after login

以上为一个基本curl访问的方法,由于这里需要通过使用IPv6的方式,所以加了相应的选项,相信大家能看的明白,平时经常用到的选项上面都有出现,大家根据需要取舍。

状态码提示405/Method Not Allowed表示不支持请求的方法,这个错误并不常见。

导致这个错误是要是由于curl默认是采用post方式进行提交访问的,post方式在此类域名下是没有权限的,比如在测试www.amazon.cn的时候就出现了这类问题,而修改为get的方式,并且增加了header头后,即可正常访问,个人推测,或许是亚马逊那边基本上都是采用get的方式,才会被认为是人为的点击,对post做了相应屏蔽。

对此增加了如下代码:

//设置Header头
$header[] = "Accept: application/json";
$header[] = "Accept-Encoding: gzip";
//添加HTTP header头采用压缩和GET方式请求
curl_setopt( $ch, CURLOPT_HTTPHEADER, $header );
curl_setopt($ch,CURLOPT_ENCODING , "gzip");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
Copy after login

命令行的形式为:

curl -v www.amazon.cn
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What to do if the Bootstrap Table uses AJAX to get data garbled What to do if the Bootstrap Table uses AJAX to get data garbled Apr 07, 2025 am 11:54 AM

Solutions to the garbled code of Bootstrap Table when using AJAX to obtain data from the server: 1. Set the correct character encoding of the server-side code (such as UTF-8). 2. Set the request header in the AJAX request and specify the accepted character encoding (Accept-Charset). 3. Use the "unescape" converter of the Bootstrap Table to decode the escaped HTML entity into original characters.

How to remove the default style in Bootstrap list? How to remove the default style in Bootstrap list? Apr 07, 2025 am 10:18 AM

The default style of the Bootstrap list can be removed with CSS override. Use more specific CSS rules and selectors, follow the "proximity principle" and "weight principle", overriding the Bootstrap default style. To avoid style conflicts, more targeted selectors can be used. If the override is unsuccessful, adjust the weight of the custom CSS. At the same time, pay attention to performance optimization, avoid overuse of !important, and write concise and efficient CSS code.

Navicat's solution to the database cannot be connected Navicat's solution to the database cannot be connected Apr 08, 2025 pm 11:12 PM

The following steps can be used to resolve the problem that Navicat cannot connect to the database: Check the server connection, make sure the server is running, address and port correctly, and the firewall allows connections. Verify the login information and confirm that the user name, password and permissions are correct. Check network connections and troubleshoot network problems such as router or firewall failures. Disable SSL connections, which may not be supported by some servers. Check the database version to make sure the Navicat version is compatible with the target database. Adjust the connection timeout, and for remote or slower connections, increase the connection timeout timeout. Other workarounds, if the above steps are not working, you can try restarting the software, using a different connection driver, or consulting the database administrator or official Navicat support.

Solutions to the errors reported by MySQL on a specific system version Solutions to the errors reported by MySQL on a specific system version Apr 08, 2025 am 11:54 AM

The solution to MySQL installation error is: 1. Carefully check the system environment to ensure that the MySQL dependency library requirements are met. Different operating systems and version requirements are different; 2. Carefully read the error message and take corresponding measures according to prompts (such as missing library files or insufficient permissions), such as installing dependencies or using sudo commands; 3. If necessary, try to install the source code and carefully check the compilation log, but this requires a certain amount of Linux knowledge and experience. The key to ultimately solving the problem is to carefully check the system environment and error information, and refer to the official documents.

How to solve mysql cannot connect to local host How to solve mysql cannot connect to local host Apr 08, 2025 pm 02:24 PM

The MySQL connection may be due to the following reasons: MySQL service is not started, the firewall intercepts the connection, the port number is incorrect, the user name or password is incorrect, the listening address in my.cnf is improperly configured, etc. The troubleshooting steps include: 1. Check whether the MySQL service is running; 2. Adjust the firewall settings to allow MySQL to listen to port 3306; 3. Confirm that the port number is consistent with the actual port number; 4. Check whether the user name and password are correct; 5. Make sure the bind-address settings in my.cnf are correct.

Unable to log in to mysql as root Unable to log in to mysql as root Apr 08, 2025 pm 04:54 PM

The main reasons why you cannot log in to MySQL as root are permission problems, configuration file errors, password inconsistent, socket file problems, or firewall interception. The solution includes: check whether the bind-address parameter in the configuration file is configured correctly. Check whether the root user permissions have been modified or deleted and reset. Verify that the password is accurate, including case and special characters. Check socket file permission settings and paths. Check that the firewall blocks connections to the MySQL server.

How to use export default in Vue How to use export default in Vue Apr 07, 2025 pm 07:21 PM

Export default in Vue reveals: Default export, import the entire module at one time, without specifying a name. Components are converted into modules at compile time, and available modules are packaged through the build tool. It can be combined with named exports and export other content, such as constants or functions. Frequently asked questions include circular dependencies, path errors, and build errors, requiring careful examination of the code and import statements. Best practices include code segmentation, readability, and component reuse.

How to solve the problem of garbled code in PHP and Bootstrap Table How to solve the problem of garbled code in PHP and Bootstrap Table Apr 07, 2025 am 11:27 AM

Solutions to display Chinese garbled code with Bootstrap Table: 1. Set the PHP character set to UTF-8; 2. Set the character set in the PHP script; 3. Make sure the database character set is UTF-8; 4. Set the character set of the Bootstrap Table to "zh-CN"; 5. Use mbstring to extend cast character set; 6. Transcode data from other encodings; 7. Check browser encoding.

See all articles