lazarantal Posted April 2, 2013 Share Posted April 2, 2013 Hi,I am trying to use the LIST command provided by Google for IMAP (as an extension).If I connect to gmail imap from linux telnet and send this command from the console, I get the following result:[#####@#### ~]$ telnet-ssl -z ssl imap.gmail.com 993Trying 64.233.183.111...Connected to gmail-imap.l.google.com.Escape character is '^]'.* OK Gimap ready for requests from 81.201.58.35 f4if1005730nfh.691 LIST "" "*"* LIST (\HasNoChildren) "/" "INBOX"* LIST (\Noselect \HasChildren) "/" "[Gmail]"* LIST (\HasNoChildren \All) "/" "[Gmail]/All Mail"* LIST (\HasNoChildren \Drafts) "/" "[Gmail]/Drafts"* LIST (\HasNoChildren \Important) "/" "[Gmail]/Important"* LIST (\HasNoChildren \Sent) "/" "[Gmail]/Sent Mail"* LIST (\HasNoChildren \Junk) "/" "[Gmail]/Spam"* LIST (\HasNoChildren \Flagged) "/" "[Gmail]/Starred"* LIST (\HasNoChildren \Trash) "/" "[Gmail]/Trash"1 OK SuccessIf I am sending this command from php, I am receiving only the last line:* OK Gimap ready for requests from 212.51.93.33 e8if455192eeo.69 * CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE ENABLE MOVE C01 OK ###########@gmail.com ######## authenticated (Success) C02 OK SuccessMy php code is the following:fwrite($fp, "C01 LOGIN username password\r\n");while ($line = fgets($fp)) {echo $line;$line = preg_split('/\s+/', $line, 0, PREG_SPLIT_NO_EMPTY);$code = $line[0];if (strtoupper($code) == 'C01') {break;}}fwrite($fp, "C02 LIST '' '*'\r\n");while ($line2 = fgets($fp)){echo $line2;$line2 = preg_split('/\s+/', $line2, 0, PREG_SPLIT_NO_EMPTY);$code = $line2[0];if (strtoupper($code) == 'C02') {break;}}What am I doing wrong?Regards,Tony Quote Link to comment https://forums.phpfreaks.com/topic/276432-reading-from-socket/ Share on other sites More sharing options...
jazzman1 Posted April 2, 2013 Share Posted April 2, 2013 Are you able to write this content down into your own file? Quote Link to comment https://forums.phpfreaks.com/topic/276432-reading-from-socket/#findComment-1422453 Share on other sites More sharing options...
lazarantal Posted April 2, 2013 Author Share Posted April 2, 2013 Are you able to write this content down into your own file? Which one? The one from the Linux console, or the one returned by the php script? Quote Link to comment https://forums.phpfreaks.com/topic/276432-reading-from-socket/#findComment-1422465 Share on other sites More sharing options...
jazzman1 Posted April 2, 2013 Share Posted April 2, 2013 (edited) The one from the Linux console, you need to write down the content into a new file on your server and then to handle it with php. Edited April 2, 2013 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/276432-reading-from-socket/#findComment-1422476 Share on other sites More sharing options...
kicken Posted April 2, 2013 Share Posted April 2, 2013 You can't just do a simple while(fgets()) loop when reading your socket. fgets() will block if there is no data to be read meaning your script will just halt on that first loop waiting for more data when what you really need to do is quit that loop and send the next command. To do that your best bet is to put the socket connection into non-blocking mode with stream_set_blocking and change your loop to correctly determine if fgets returned data or not. If there is no data available I believe it would return false, but a little testing should figure that out for sure. There's actually a fair amount to consider when dealing with two-way socket communication and ensuring you handle the data properly. You might be able to hack together something that works for your needs but you may be better off doing some googleing to try and find a pre-written library to do what you want. Quote Link to comment https://forums.phpfreaks.com/topic/276432-reading-from-socket/#findComment-1422492 Share on other sites More sharing options...
lazarantal Posted April 3, 2013 Author Share Posted April 3, 2013 Thank you for the advices! @jazzman1 - As the folder structure returned by the Gmail LIST command may vary from user to user, it is not feasable to write the linux console's content into a file, and read it from there each time it's needed. That is why I am trying to execute the command from php and process the result in real time. @kicken - You are right, fgets is stoped and blocked if no data can be read from the socket. However in my case the fgets is getting stopped (not blocked) when the loop is reaching the point when there is no more data provided. According to the documentations and according to the linux console response my php script should get (and echo) the following: C02 LIST "" "*"* LIST (\HasNoChildren) "/" "INBOX"* LIST (\Noselect \HasChildren) "/" "[Gmail]"* LIST (\HasNoChildren \All) "/" "[Gmail]/All Mail"* LIST (\HasNoChildren \Drafts) "/" "[Gmail]/Drafts"* LIST (\HasNoChildren \Important) "/" "[Gmail]/Important"* LIST (\HasNoChildren \Sent) "/" "[Gmail]/Sent Mail"* LIST (\HasNoChildren \Junk) "/" "[Gmail]/Spam"* LIST (\HasNoChildren \Flagged) "/" "[Gmail]/Starred"* LIST (\HasNoChildren \Trash) "/" "[Gmail]/Trash"C02 OK Success I know that the fgets is not blocked because the last line is printed (which is also the stop condition): C02 OK Success For some reason my php script is not getting or not printing the lines before the last one (All the starred lines). Quote Link to comment https://forums.phpfreaks.com/topic/276432-reading-from-socket/#findComment-1422623 Share on other sites More sharing options...
Solution salathe Posted April 3, 2013 Solution Share Posted April 3, 2013 (edited) The LIST command that you're sending via the PHP script is different to that that is expected (and the one you demonstrated with telnet). The PHP script sends: C02 LIST '' '*' When it should send: C02 LIST "" "*" See the difference? Something like the following would suffice: fwrite($fp, 'C02 LIST "" "*"' . "\r\n"); Edited April 3, 2013 by salathe Quote Link to comment https://forums.phpfreaks.com/topic/276432-reading-from-socket/#findComment-1422719 Share on other sites More sharing options...
lazarantal Posted April 3, 2013 Author Share Posted April 3, 2013 The LIST command that you're sending via the PHP script is different to that that is expected (and the one you demonstrated with telnet). The PHP script sends: C02 LIST '' '*' When it should send: C02 LIST "" "*" See the difference? Something like the following would suffice: fwrite($fp, 'C02 LIST "" "*"' . "\r\n"); You are right, I've exchanged the single and double quotes and now I get the full response. I would not have thinked that this might cause the problem, the server returned a "Successfull" response (not a "bad command" one) Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/276432-reading-from-socket/#findComment-1422729 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.