Page 1 of 1

[SOLVED] Reading from fsockopen() creates infinite loop?

Posted: Sun Feb 15, 2009 10:03 am
by s.dot

Code: Select all

<?php
 
if ($fileStream2 = fsockopen('www.example.com', 80, $errno, $errstr, 30))
{
    //connection opened
    echo '<p>Socket connection opened<br />';
    
    //check out the filestream info
    echo '$fileStream2 Info: ' . var_dump($fileStream2);
    
    //get headers
    $responseHeaders = get_headers('http://www.example.com/script.php');
    
    //headers text
    echo 'Response headers received<br>';
    
    if (stripos($responseHeaders[0], '200 OK'))
    {
        //received good headers
        echo 'Received 200 OK<br>';
        
        //write the data to the file stream pointer
        if (fwrite($fileStream2, $headers . $rsRequest))
        {
            echo 'Successfully wrote to the socket stream<br>';
        }
        
        //start reading contents
        echo 'Reading contents received<br>';
        
        while (!feof($fileStream2))
        {
            //err?
            echo 'reading';
            $contents .= fgets($fileStream2, 1024);
        }
        
        //what do we have?
        echo 'Received contents: ' . $contents;
        fclose($fileStream2);
    } else
    {
        echo 'not writing';
        $rsSent = false;
    }
}
Everything happens as expected up until this code:

Code: Select all

while (!feof($fileStream2))
{
    //err?
    echo 'reading';
    $contents .= fgets($fileStream2, 1024);
}
I've read that feof() will never return true on an invalid file pointer but my debug info in the script above shows that $fileStream2 is 'resource(12) of type (stream)', so it looks valid to me.

I can never get $contents because the page just continues in an infinite loop (and doesn't obey the 30 second timeout of my fsockopen() call).

I can't figure out why feof() is never returning true (I assume that is the problem). If i put exit; directly after the while (), it still goes into an infinite loop.

Can anyone help me out?

EDIT| This is the verbose output I am getting from my script:

Code: Select all

Socket connection opened
$fileStream2 Info: resource(12) of type (stream) 
Response headers received
Received 200 OK
Successfully wrote to the socket stream
Reading contents received
(insert hang here :P)
And on another note, it may not be feof() not returning true, because if i put $contents = fgets($fileStream2, 1024) before the while() loop.. it still hangs!

So.. hmmm it may be fgets()?

Re: Reading from fsockopen() creates infinite loop?

Posted: Sun Feb 15, 2009 10:11 am
by John Cartwright
Try only looping x amount of time's before dieing to see what is being returned (if anything). It sounds like the socket is never finishing it's response, and/or taking too long.

Re: Reading from fsockopen() creates infinite loop?

Posted: Sun Feb 15, 2009 10:17 am
by s.dot
Well, even if I put the following lines before the loop:

Code: Select all

$contents = fgets($fileStream2, 1024);
exit;
The page hangs and never finishes.

Re: Reading from fsockopen() creates infinite loop?

Posted: Mon Feb 16, 2009 12:17 am
by s.dot
OK, figured it out.
I was sending $headers when the variable was supposed to be $headers2

This allowed me to get inside of the loop but I could not read, still.

So I added

Code: Select all

stream_set_blocking($fileStream2, false);
stream_set_timeout($fileStream2, 30);
And now it works. :)

Re: [SOLVED] Reading from fsockopen() creates infinite loop?

Posted: Sat Feb 28, 2009 9:47 pm
by John Cartwright
A bit late to respond on this..

Just goes to show why non-descriptive variable names should be avoided. :)