michellylei Posted July 9, 2009 Share Posted July 9, 2009 <?php $fp = fsockopen("www.example.com", 80, $errno, $errstr, 30); if (!$fp) { echo "$errstr ($errno)<br />\n"; } else { $out = "GET / HTTP/1.1\r\n"; $out .= "Host: www.example.com\r\n"; $out .= "Connection: Close\r\n\r\n"; fwrite($fp, $out); while (!feof($fp)) { echo fgets($fp, 128); } fclose($fp); } ?> 1, when fwrite called, how many msg reply from www.example.com? why use loop to read? 2, can i call fwrite twice first, then call fget twice to get feedback? Link to comment https://forums.phpfreaks.com/topic/165366-fsockopen/ Share on other sites More sharing options...
niwa3836 Posted July 9, 2009 Share Posted July 9, 2009 Not sure I understand the question, but will try and answer as best... 1, when fwrite called, how many msg reply from www.example.com? why use loop to read? Answer> it will continue to read until that buffer is clear 2, can i call fwrite twice first, then call fget twice to get feedback? Answer> You are calling fgets lots until the buffer is cleared. You are entering a little bit of an interesting area here, I set the streams to non blocking and then have a watchdog timer that quits if no data arrived in a while, therefore even if the buffer is initiallly cleared I can still wait, just in case the app sends a little bit more. stream_set_blocking($conn,0) ; if($starttime + 20 < date('U')) break ; if(!feof($conn)) { $result = fgets($conn) ; Thats a little snippett, which might give you an idea (the break leaves a while loop) Link to comment https://forums.phpfreaks.com/topic/165366-fsockopen/#findComment-872191 Share on other sites More sharing options...
michellylei Posted July 13, 2009 Author Share Posted July 13, 2009 thanks so much. so if i do this way: fwrite($fp, $out); fwrite($fp, $out1); fwrite($fp, $out2); while (!feof($fp)) { echo fgets($fp, 128); } so fgets will read all infomation which reply to 3 fwrite call, right? but u can not know which data are reply to which fwrite call,right? Link to comment https://forums.phpfreaks.com/topic/165366-fsockopen/#findComment-874650 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.