ryy705 Posted November 20, 2008 Share Posted November 20, 2008 Hello, Could someone kindly help me with the following question? I ran it from the command and it does indeed hang. But I don't know the reason. How is the "request blocking" ? What is wrong with the following code snippet? Assume default configuration values apply. <?php $fp = fsockopen('www.php.net', 80); fwrite($fp, "GET / HTTP/1.0\r\nHost: www.php.net\r\n"); $data = fread($fp, 8192); ?> Answer... The request is blocking and may cause fread() to hang The HTTP request is malformed This script should be re-written using fgets() instead of fread() The request is non-blocking and fread() may miss the response You cannot use fwrite() with fsockopen() Link to comment https://forums.phpfreaks.com/topic/133459-solved-another-quiz-qestion/ Share on other sites More sharing options...
ratcateme Posted November 20, 2008 Share Posted November 20, 2008 when you make a http request you send headers in your case the headers are "GET / HTTP/1.0\r\nHost: www.php.net\r\n" each header is separated with a \r\n (Windows new line) but to tell the server you have finished sending the headers you need to send a \r\n by its self to tell it you have finished and to start sending the web page. your headers should be "GET / HTTP/1.0\r\nHost: www.php.net\r\n\r\n" also be aware that is a very basic request and some servers may want you to provide more details in your request. Scott. Link to comment https://forums.phpfreaks.com/topic/133459-solved-another-quiz-qestion/#findComment-694163 Share on other sites More sharing options...
ryy705 Posted November 20, 2008 Author Share Posted November 20, 2008 thx Link to comment https://forums.phpfreaks.com/topic/133459-solved-another-quiz-qestion/#findComment-694518 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.