Jump to content

[SOLVED] Another quiz qestion


ryy705

Recommended Posts

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.