Re: fsockopen on ssl://

[email protected] (Manuel Lemos)
Newsgroups php.general
Message-ID <[email protected]>
Hello,

on 05/13/2008 04:37 PM bob pilly said the following:
> Hi all
> 
> I have tried researching this issue but havent come up with any solution so im hoping someone has seen it before and can help. I have the following test script that uses fsockopen to connect to a https site, get the contents and outputs it.
> 
> <?php
> $host = "www.microsoft.com";
> $path = "/";
> $fh = fsockopen("ssl://".$host, 443, $errno, $errstr, 5);//opens url for reading with a timeout of 2 seconds
> 
> if (!$fh){
>     echo "FAIL: $errno $errstr ";
> }
> else{
>     $out = "GET $path HTTP/1.1\r\n";
>     $out .= "Host: $host\r\n";
>     $out .= "Connection: Close\r\n";
>     $out .= "\r\n";
>     fwrite($fh, $out);
>     stream_set_timeout($fh,2);
>     $info = stream_get_meta_data($fh);
>     if($info['timed_out']){
>         echo "TIMEOUT\n";
>     }
>     else{
>         $haystack = "";
>         while (!feof($fh)) {
>             $haystack.= fgets($fh, 4096);
>         }
>     }
>     print $haystack;
>     fclose($fh);
> }
> ?>
> 
> if i run this script using php -f test.php it works fine. However if i try and run this on my loca apache server i get the following error:
> 
> Warning:  fsockopen() [function.fsockopen]:unable to connect to ssl://www.microsoft.com:443 (A connection attemptfailed because the connected party did not properly respond after aperiod of time, or established connection failed because connected hosthas failed to respond.) in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\test.php on line 4
> FAIL: 10060 A connection attempt failed because the connected party didnot properly respond after a period of time, or established connectionfailed because connected host has failed to respond.
> 
> As you can see from that error i am using windows and apache 2.2. My php version is 5.25. I have Registered Stream Socket Transports tcp, udp, ssl, sslv3, sslv2, tlsin my config.

I suspect that you are giving a very short timeout but then you are not
handling the timeout error properly.

Anyway, before reinventing the wheel, you may to try this HTTP client
class that supports many options including establishing SSL corrections
and setting and handling timeouts correctly.

http://www.phpclasses.org/httpclient

-- 

Regards,
Manuel Lemos

PHP professionals looking for PHP jobs
http://www.phpclasses.org/professionals/

PHP Classes - Free ready to use OOP components written in PHP
http://www.phpclasses.org/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.