While I'm trying to send a GET
request to an address with php
I receive HTTP 400 BAD REQUEST
message and I can't figure out why.
Here's my code:
function redirect($url)
{
error_reporting(E_ERROR | E_PARSE);
$components = parse_url($url);
$port = $components["port"];
$ip = $components["host"];
$path = $components["path"];
//create and connect socket with the parameters entered by the user
//$sock = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
echo "Establishing connection to the given adress...
";
//Connection timeout limit is set to 10 seconds...
if(!isset($port))
{
$port = 80;
}
$sock = fsockopen($ip, $port,$errno, $errstr, 10) or die("Unable to connect...");
$request = "GET $path HTTP/1.1" . "
";
fwrite($sock, $request);
while ($header = stream_get_line($sock, 1024, "
")) {
$response.= $header . "
";
}
echo $response;
$loc = "";
if (preg_match("/Location:\'(.*)\
/", $response, $results))
$loc = $results[1];
echo $loc;
}
Any suggestions?