PHPCrawl fails to create SSL socket

丶灬走出姿态 提交于 2019-12-11 04:08:30

问题


I'm trying to use PHPCrawl (http://sourceforge.net/projects/phpcrawl/) to trawl a website delivered over HTTPS.

I can see that there is support for SSL in the PHPCrawlerHTTPRequest class (openSocket method):

// 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);
  }

The problem lies in the call to stream_socket_client - although it returns a zero error_code, and no error_str, this->socket is still false.

The documentation for the method states the following:

If the value returned in errno is 0 and the function returned FALSE, it is an indication that the error occurred before the connect() call.

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

So I've tried to use an example provided in the comments section to modify the stream context using 'stream_context_set_option' to set verify_host and verify_peer to false - neither of which seems to have any effect.

I'm not very proficient in PHP or the intricacies of web - does anyone know either:

  • What condition (specifically) can cause this call to fail? OR
  • A workaround for the issue?

I should note - I am using Facebook (HTTPS) as the test server.


回答1:


I've found the issue -

  • PHP versions 5.6.x turn peer verification on by default, and apparently the necesarry cert isn't found sometimes (see this bug report)

  • The workaround is to drop back to a PHP version prior to 5.6




回答2:


Old topic, but I had the same problem using the PHPCrawler. What worked for me, is what an user wrote on sourceforge (Source: https://sourceforge.net/p/phpcrawl/bugs/86/#5993).

What you have to do, is to rewrite the stream_context_create call on line 547 in PHPCrawlerHTTPReqeust.class.php into the following:

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

Hope this helps someone in the future.



来源:https://stackoverflow.com/questions/29403231/phpcrawl-fails-to-create-ssl-socket

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!