CURL错误:140770FC:SSL例程:SSL23_GET_SERVER_HELLO:未知协议

时间:2018-10-21 18:43:59

标签: 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

该如何解决?

1 个答案:

答案 0 :(得分:1)

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

您正在尝试访问URL https://www.stubhub.com/-这意味着端口443(协议默认为https)上的给定主机具有协议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
  

该如何解决?

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

  

此选项将number设置为要连接的远程端口号,而不是 URL中指定的端口号或所用协议的默认端口。通常,您只需让URL决定即可使用哪个端口,但这可以使应用程序覆盖它。