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
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 1

https 能使用 http 代理吗

2年前 评论

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