【问题标题】:CURL error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocolCURL 错误:140770FC:SSL 例程:SSL23_GET_SERVER_HELLO:未知协议
【发布时间】:2018-10-21 18:43:58
【问题描述】:

我正在尝试将代理和 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

如何解决这个问题?

【问题讨论】:

    标签: php ssl curl web-scraping proxy


    【解决方案1】:
    * Rebuilt URL to: https://www.stubhub.com/
    

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

    但是,由于未知原因,您明确指定应使用端口 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 决定使用哪个端口,但这允许应用程序覆盖它。

    【讨论】:

      猜你喜欢
      • 2015-11-29
      • 2014-02-03
      • 2019-03-12
      • 2014-08-05
      • 1970-01-01
      • 2016-01-11
      • 2023-03-31
      • 2014-04-22
      • 1970-01-01
      相关资源
      最近更新 更多