php - curl 错误 :140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol

标签 php ssl curl web-scraping proxy

我正在尝试将代理和 SSL 证书与 CURL 一起使用,但出现错误。

这是 CURL 代码:

//Website
$url = 'https://www.stubhub.com';

//Curl
$curl=curl_init();

//SSL
curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, true );
curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, 2 );
curl_setopt( $curl, CURLOPT_CAINFO, 'C:\xampp\cacert.pem' );

curl_setopt( $curl, CURLOPT_URL,trim( $url ) );
curl_setopt($curl, CURLOPT_REFERER, $url);
curl_setopt( $curl, CURLOPT_AUTOREFERER, true );
curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $curl, CURLOPT_FAILONERROR, true );
curl_setopt( $curl, CURLOPT_HEADER, false );
curl_setopt( $curl, CURLINFO_HEADER_OUT, false );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $curl, CURLOPT_BINARYTRANSFER, true );
curl_setopt( $curl, CURLOPT_CONNECTTIMEOUT, 0 );
curl_setopt( $curl, CURLOPT_TIMEOUT, 0 );
curl_setopt( $curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36' );

//Proxy
curl_setopt($curl, CURLOPT_HTTPPROXYTUNNEL, 1);
curl_setopt($curl, CURLOPT_PROXY, '177.190.147.241:41545');
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt ($curl, CURLOPT_PORT , 80);

curl_setopt($curl, CURLOPT_COOKIEFILE,__DIR__."/cookie.txt");
curl_setopt( $curl, CURLOPT_MAXREDIRS, 10 );
curl_setopt( $curl, CURLOPT_ENCODING, '' );

curl_setopt( $curl, CURLOPT_VERBOSE, true );
curl_setopt( $curl, CURLOPT_NOPROGRESS, true );
curl_setopt( $curl, CURLOPT_STDERR, $vbh );

我得到以下响应:

* Rebuilt URL to: https://www.stubhub.com/
*   Trying 177.190.147.241...
* TCP_NODELAY set
* Connected to 177.190.147.241 (177.190.147.241) port 41545 (#0)
* Establish HTTP proxy tunnel to www.stubhub.com:80
> CONNECT www.stubhub.com:80 HTTP/1.1
Host: www.stubhub.com:80
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1
Proxy-Connection: Keep-Alive

< HTTP/1.1 200 OK
< 
* Proxy replied OK to CONNECT request
* ALPN, offering http/1.1
* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
* successfully set certificate verify locations:
*   CAfile: C:\xampp\cacert.pem
  CApath: none
* error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol
* Curl_http_done: called premature == 1
* Closing connection 0
error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol

如何解决这个问题?

最佳答案

* Rebuilt URL to: https://www.stubhub.com/

您正在尝试访问 URL https://www.stubhub.com/ - 这意味着端口 443 上的给定主机(默认为 https)协议(protocol)为 https(即基于 TLS 的 HTTP) .

但是,由于未知原因,您明确指定应使用端口 80 而不是端口 443:

curl_setopt ($curl, CURLOPT_PORT , 80);

这意味着它不会连接到主机的 443 端口,而是 80 端口:

* Establish HTTP proxy tunnel to www.stubhub.com:80
> CONNECT www.stubhub.com:80 HTTP/1.1

端口 80 用于纯 HTTP。然而,客户端将尝试 https(基于 TLS 的 HTTP),因为这是 URL 所说的。因此,客户端将尝试通过发送 ClientHello 来启动 TLS 握手。由于服务器需要一个普通的 HTTP 请求,但却得到了一个 TLS ClientHello,它会用 HTTP 错误响应进行回复。然后客户端将尝试将此响应解析为预期的 TLS 响应并失败:

error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol

How to fix this?

不要将 CURLOPT_PORT 设置为 80。只是不要设置 CURLOPT_PORT 以便它从 URL 中获取。来自documentation :

This option sets number to be the remote port number to connect to, instead of the one specified in the URL or the default port for the used protocol. Usually, you just let the URL decide which port to use but this allows the application to override that.

关于php - curl 错误 :140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52918661/

相关文章:

symfony - 我无法让 https 使用 symfony 2

c - OpenSSL X509_Sign "invalid digital signature"

php - 通过 Facebook 的图形 API 发布状态

php - 使用通配符删除字符串中的数据

python-3.x - 如何使用 Python 创建和连接匿名 DH TLS 套接字,并通过它们安全地进行身份验证,而无需进行凭证转发?

php - 让 PHP 等到函数完成?

php - 需要 CURLOPT_CAINFO 吗?

curl - 如何在 curl 中使用 HTTP/2 的推送功能?

Windows 10 上的 PHP curl,PHP 5.6.17

php - 无法从数据库获取图像和信息