php stream_socket_client timeout does not work as expected

3.9k views Asked by At

i have written a class which checks ssl vertificates via php stream_socket_client.

however timeout does not work for some sites. the scripts sometimes wait up to 60 seconds for response of some hosts (seems to be a php configuration limit).

this is confusing because i set the (connect) timeout to 2 seconds. for the ssl transport i do not see any other limits i can set to reduce the max connection time/ transfer time. are there any other time limits i can use?

    $options = array( 
        "ssl" => array(
        "capture_peer_cert"       => true,
        "capture_peer_chain"      => true,
        "capture_peer_cert_chain" => true, 
        "verify_peer"             => $verify_peer,  
        "allow_self_signed"       => $allow_self_signed )
    );

    if (strlen($this->cafile) > 0) {
        $options['ssl']['cafile'] = $this->cafile;
        if (strlen($this->capath) > 0 && [..]) {
            $options['ssl']['capath'] = $this->capath;
        }
    } else {
        $options['verify_peer'] = false;
    }

    $get = @stream_context_create( $options);

    $read = @stream_socket_client("ssl://$host:443", $errno, $errstr, 2, STREAM_CLIENT_CONNECT, $get);

    if($read){
        stream_set_timeout($read, 2);
        $cert = stream_context_get_params($read);
    } else {
        $cert = false;
    }       
1

There are 1 answers

0
Mahbub On

Check the http://php.net/manual/en/function.stream-set-timeout.php for Dianoga (dianoga7 [at] 3dgo.net) response.