guzzle使用代理请求https接口,出现400

1. 运行环境

php8+guzzle7.5

1). 当前使用的 Laravel 版本?

laravel 8.5

2. 问题描述?

当使用代理IP访问https请求的时候,会出现400错误。
但同样配置访问http请求,不会出现错误,得到正常结果
配置如下:

public function request($method, $url, $data = null)
    {
        $options = [
            "body" => $data,
            "http_errors" => false,
            "verify" => $this->isHttps($url),
            'timeout' => 30,
            'debug' => true,
        ];

        //尝试使用代理IP抓取
        $options['proxy'] = 'http://58.246.58.150:9002';
        $response = $this->connector->request($method, $url, $options);

        if ($response->getStatusCode() == "200") {
            return $response->getBody()->getContents();
        }

        throw new SpiderException("[" . $response->getStatusCode() . "]请求失败!", $url, $response->getStatusCode());
    }

    //伪代码
    //报400错误
    $this->request('get', 'https://baidu.com');
    //能正常返回
    $this->request('get', 'http://baidu.com');

3. 您期望得到的结果?

返回正常的请求结果

4. 您实际得到的结果?

*   Trying 58.246.58.150...
* TCP_NODELAY set
* Connected to 58.246.58.150 (58.246.58.150) port 9002 (#0)
* allocate connect buffer!
* Establish HTTP proxy tunnel to baidu.com:443
> CONNECT baidu.com:443 HTTP/1.1
Host: baidu.com:443
Proxy-Connection: Keep-Alive

< HTTP/1.1 400 Bad Request
< Server: nginx
< Date: Thu, 25 Aug 2022 07:36:08 GMT
< Content-Type: text/html
< Content-Length: 166
< Connection: close
< 
* Received HTTP code 400 from proxy after CONNECT
* CONNECT phase completed!
* Closing connection 0
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 1

https 能使用 http 代理吗

2年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!