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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

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

Post 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()?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Reading from fsockopen() creates infinite loop?

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Reading from fsockopen() creates infinite loop?

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Reading from fsockopen() creates infinite loop?

Post 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. :)
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

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

Post by John Cartwright »

A bit late to respond on this..

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