【问题标题】:PHPCrawl fails to create SSL socketPHPCrawl 无法创建 SSL 套接字
【发布时间】:2015-06-06 20:01:26
【问题描述】:

我正在尝试使用 PHPCrawl (http://sourceforge.net/projects/phpcrawl/) 来拖网通过 HTTPS 交付的网站。

可以看到在PHPCrawlerHTTPRequest类(openSocket方法)中有对SSL的支持:

// If ssl -> perform Server name indication
  if ($this->url_parts["protocol"] == "https://")
  {
    $context = stream_context_create(array('ssl' => array('SNI_server_name' => $this->url_parts["host"])));
    $this->socket = @stream_socket_client($protocol_prefix.$ip_address.":".$this->url_parts["port"], $error_code, $error_str,
                                          $this->socketConnectTimeout, STREAM_CLIENT_CONNECT, $context);
  }

问题在于对stream_socket_client的调用——虽然它返回一个零error_code,并且没有error_str,this->socket仍然是假的。

该方法的文档说明如下:

如果 errno 中返回的值为 0 并且函数返回 FALSE,则表明错误发生在 connect() 调用之前。

(见http://php.net/manual/en/function.stream-socket-client.php

所以我尝试使用 cmets 部分中提供的示例来修改流上下文,使用 'stream_context_set_option' 将 verify_host 和 verify_peer 设置为 false - 这两者似乎都没有任何效果。

我对 PHP 或网络的复杂性不是很精通 - 有人知道吗:

  • 什么条件(具体而言)会导致此调用失败? 或
  • 该问题的解决方法?

我应该注意 - 我使用 Facebook (HTTPS) 作为测试服务器。

【问题讨论】:

    标签: php sockets ssl https web-crawler


    【解决方案1】:

    我找到了问题 -

    • PHP 5.6.x 版本默认开启对等验证,显然有时找不到必要的证书 (see this bug report)

    • 解决方法是退回到 5.6 之前的 PHP 版本

    【讨论】:

    • 无需退回 PHP 版本。有关一些解决方案,请参阅this link。或者,只需使用此处找到的代码覆盖 PHPCrawlerHTTPRequest.class.php - github.com/merzilla/phpcrawl/blob/fix-php56-ssl-problem/libs/…
    • @MohamedOmar 太棒了!谢谢你让我知道。很高兴听到关于我近 2 年前写的东西的良好反馈。 :-D 如果有帮助,也许我应该将此评论作为正确答案发布..
    【解决方案2】:

    老话题,但我在使用 PHPCrawler 时遇到了同样的问题。对我有用的是用户在 sourceforge 上写的内容(来源:https://sourceforge.net/p/phpcrawl/bugs/86/#5993)。

    你要做的就是将 PHPCrawlerHTTPReqeust.class.php 中第 547 行的 stream_context_create 调用重写为以下内容:

    $context = stream_context_create(array(
        'ssl' => array(
            'SNI_server_name' => $this->url_parts["host"],
            'verify_peer' => false,
            'verify_peer_name' => false,
        )
    ));
    

    希望这对将来的某人有所帮助。

    【讨论】:

      猜你喜欢
      • 2010-11-04
      • 2022-01-18
      • 1970-01-01
      • 2011-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-25
      • 2014-01-18
      相关资源
      最近更新 更多