cURL error 35: TLS connect error: error:0A000126:SSL routines::unexpected eof while reading
用Guzzle抓取网页
public function fetch(string $url): string
{
$client = new Client([
'headers' => [
'User-Agent' => 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
],
'allow_redirects' => true,
'http_errors' => false,
'timeout' => 30,
]);
$response = $client->request('GET', $url);
return $response->getBody()->getContents();
}是出现这个错误
GuzzleHttp\Exception\ConnectException cURL error 35: TLS connect error: error:0A000126:SSL routines::unexpected eof while reading (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
开始以为是ssl证书验证失败,加了 'verify' => false,但问题依然没有解决
为什么 verify => false 也没用?
这个不是: 证书验证失败,而是:TLS 握手过程被服务器主动中断。所以关闭证书校验没意义。
后来发现是cookie问题,应该是资源方对cookie做了限制,改成下面这样后问题就解决了
public function fetch(string $url): string
{
$client = new Client([
'cookies' => true,
'headers' => [
'User-Agent' => 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
],
'allow_redirects' => true,
'http_errors' => false,
'timeout' => 30,
]);
$response = $client->request('GET', $url);
return $response->getBody()->getContents();
}