Reading data from fsockopen using fgets/fread hangs

23,324

Solution 1

Your script is hanging in the while loop at the end. This is because you have used !feof() as the condition for the loop, and the server is not closing the connection. This means the feof() will always return false and the loop will continue forever.

This will not be problem when your write a full implementation, as you will be looking for response codes and can break out of the loop accordingly, for example:

<?php

  // Open a socket
  if (!($fp = fsockopen('ssl://imap.gmail.com', 993, $errno, $errstr, 15))) {
      die("Could not connect to host");
  }

  // Set timout to 1 second
  if (!stream_set_timeout($fp, 1)) die("Could not set timeout");

  // Fetch first line of response and echo it
  echo fgets($fp);

  // Send data to server
  echo "Writing data...";
  fwrite($fp, "C01 CAPABILITY\r\n");
  echo " Done\r\n";

  // Keep fetching lines until response code is correct
  while ($line = fgets($fp)) {
    echo $line;
    $line = preg_split('/\s+/', $line, 0, PREG_SPLIT_NO_EMPTY);
    $code = $line[0];
    if (strtoupper($code) == 'C01') {
      break;
    }
  }

  echo "I've finished!";

Solution 2

Your script should be working. In fact, it is working.

See the results below on my pc when I ran your code:

* OK Gimap ready for requests from xx.xx.xx.xx l5if4585958ebb.20
* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY  SASL-IR AUTH=XOAUTH
C01 OK Thats all she wrote! l5if4585958ebb.20

Since gmail doesn't disconnect you. No end of file occurs. And the page loading simply times out.

In other words: Your script will just keep waiting and waiting until gmail does disconnect, which unfortunately happens after your page load has already timed out.

Share:
23,324
victor_golf
Author by

victor_golf

Updated on December 08, 2020

Comments

  • victor_golf
    victor_golf over 3 years

    Here is the code that I am using:

    if (!($fp = fsockopen('ssl://imap.gmail.com', '993', $errno, $errstr, 15)))
        echo "Could not connect to host";
    $server_response = fread($fp, 256);
    echo $server_response;
    
    fwrite($fp, "C01 CAPABILITY"."\r\n");
    while (!feof($fp)) {
        echo fgets($fp, 256);
    }
    

    I get the first response:

    OK Gimap ready for requests from xx.xx.xx.xx v3if9968808ibd.15 
    

    but then the page times out. I have searched through stream_set_blocking, stream_set_timeout, stream_select, fread, etc. but could not get it to work. I need to read all the data that the server sends and then proceed with other commands (I would be retrieving emails using imap).

    Thanks

  • victor_golf
    victor_golf almost 12 years
    Did you get the result immediately, or it took some time (1-2min) to timeout??
  • Woutifier
    Woutifier almost 12 years
    The problem is probably that you are trying to run it from a webpage am I right? The page times out before gmail disconnects (so feof() never returns true). It probably loses part of the buffer in the process of timing out.
  • victor_golf
    victor_golf almost 12 years
    Yes. I have a .php page which has this code. What's the problem?
  • victor_golf
    victor_golf almost 12 years
    I receive the following response but after 2min of loading the page: * OK Gimap ready for requests from xx.xx.xx.xx vb10if10168308icb.24 * CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH C01 OK Thats all she wrote! vb10if10168308icb.24 How can I deal with the time out, since stream_set_timeout($fp,1) does not seem to work..
  • Woutifier
    Woutifier almost 12 years
    Your code works. You should look for that C01 OK instead of waiting for end of file
  • Woutifier
    Woutifier almost 12 years
    The stream does not timeout. It just sits there waiting for input/output from either gmail or you.
  • victor_golf
    victor_golf almost 12 years
    OK. But thats a solution only if the server responds as expected. What if I receive an error, the page will still hang? And as I mentioned, I will be retrieving emails so just checking for "OK" may not be a good solution. Am I right??
  • Woutifier
    Woutifier almost 12 years
    Yes you will actually have to check the protocol specifications for possible error codes and implement appropriate responses for them. After this step you can continue by sending your "question" to the gmail server.
  • victor_golf
    victor_golf almost 12 years
    Ok.Thanks. @DaveRandom has also suggested the same (code below).
  • DaveRandom
    DaveRandom almost 12 years
    RFCs are your friends ;-) (Don't forget to handle error responses as well, or you script will hang forever waiting for the C01 response it will never get)
  • victor_golf
    victor_golf almost 12 years
    Yes. I have been going through those commands for some time now. Thanks again.