代码之家  ›  专栏  ›  技术社区  ›  Brian Millot

如何修复使用curl接收数据时的故障

  •  0
  • Brian Millot  · 技术社区  · 5 年前

    我正在尝试使用CURL获取澳大利亚verison黄页主页的html代码,但是我遇到了一个CURL错误。 每次都会产生以下错误。

    * Rebuilt URL to: https://www.yellowpages.com.au/
    *   Trying 104.94.32.193...
    * TCP_NODELAY set
    * Connected to www.yellowpages.com.au (104.94.32.193) port 443 (#0)
    * ALPN, offering http/1.1
    * Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
    * successfully set certificate verify locations:
    *   CAfile: D:\projects\my_site\webroot/cacert-2019-01-23.pem
      CApath: none
    * SSL connection using TLSv1.2 / ECDHE-RSA-AES256-GCM-SHA384
    * ALPN, server accepted to use http/1.1
    * Server certificate:
    *  subject: C=AU; ST=Victoria; L=Melbourne; O=Sensis Pty Ltd; OU=SSL Administration; OU=Unified Communications; CN=*.whitepages.com.au
    *  start date: Oct  4 00:00:00 2016 GMT
    *  expire date: Sep  5 23:59:59 2019 GMT
    *  subjectAltName: host "www.yellowpages.com.au" matched cert's "*.yellowpages.com.au"
    *  issuer: C=GB; ST=Greater Manchester; L=Salford; O=COMODO CA Limited; CN=COMODO RSA Organization Validation Secure Server CA
    *  SSL certificate verify ok.
    > GET / HTTP/1.1
    Host: www.yellowpages.com.au
    User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13
    Accept: */*
    
      Curl error: SSL read: error:00000000:lib(0):func(0):reason(0), errno 10054cURL error (56):
     Failure when receiving data from the peer
    

    $url = 'https://www.yellowpages.com.au';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CAINFO, __DIR__ . "/cacert-2019-01-23.pem");
    curl_setopt($ch, CURLOPT_TIMEOUT, 0); //Wait forever
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
    //curl_setopt($ch, CURLOPT_VERBOSE, true);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13');
    $result = curl_exec($ch);
    if($errno = curl_errno($ch)) {
    echo 'Curl error: ' . curl_error($ch);
    $error_message = curl_strerror($errno);
    echo "cURL error ({$errno}):\n {$error_message}";
    
    1 回复  |  直到 5 年前
        1
  •  0
  •   Professor Abronsius    5 年前

    我发现,对于您的代码(除了缺少最后的右大括号),它需要 CURLOPT_FOLLOWLOCATION 选项集

    curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
    

    也就是说,当它运行时

    Curl错误:无法解析主机:www.yellowpages.com.aucURL错误 (6) :无法解析主机名

    Postman

    我们检测到来自您的IP的异常流量活动 地址。