PHP fsockopen 400 Bad Request

I have this strange problem: I get 400 Bad requests as an answer, and I absolutely do not understand what happened to the header. Here is 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?

thank

EDIT . Thanks to MrCode, this problem has been resolved. The problem was here:

fputs($sock, join('\r\n', $header));

I had to change it to:

fputs($sock, join("\r\n", $header)."\r\n\r\n");

Pay attention to double quotes and "\ r \ n \ r \ n"

Thanks again MrCode

+4
source share
1 answer

You are missing \r\n\r\nwhat is required after the final heading. You currently have nothing after the final headline.

Add it to the connection result:

fputs($sock, join("\r\n", $header) . "\r\n\r\n");

, \r\n, PHP , .

+4

All Articles