Reputation: 113
I have this strange problem that I'm receiving 400 Bad request as a response and I have absolutely no idea what's wrong with the header. Here's my code:
<?php
$sock = fsockopen('IP ADDRESS', 80, $errno, $errstr);
$header = array(
'GET / HTTP/1.1',
'Host: stackoverflow.com',
'User-agent: My Useragent',
'Connection: Close'
);
fputs($sock, join('\r\n', $header));
while(!feof($sock))
{
echo fgets($sock, 128);
break;
}
fclose($sock);
?>
Any ideas what I'm doing wrong?
Thanks
EDIT: Thanks to MrCode this problem was solved. Issue was here:
fputs($sock, join('\r\n', $header));
I had to change it to:
fputs($sock, join("\r\n", $header)."\r\n\r\n");
Notice double quotes and "\r\n\r\n"
Thanks again to MrCode
Upvotes: 4
Views: 2966
Reputation: 64536
You're missing \r\n\r\n
which is required after the final header. Currently you have nothing after the final header.
Append it to the join result:
fputs($sock, join("\r\n", $header) . "\r\n\r\n");
Also, you need to use double quotes around the \r\n
because using single quotes causes PHP to take it literally and not as a new line.
Upvotes: 5