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
https 能使用 http 代理吗