I'm trying to do HTTP Communication using Sockets in php.
Firstly, these are the request and response headers (captured using livehttpheaders firefox plugin) of the URL I'm trying to GET.
This is the script I'm using to do HTTP GET request. Problem is, when I run this script it just keeps on loading and hangs in there. I never got the response:GET /~*temp/~1-194-39debee1-0 HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008072820 Firefox/3.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
HTTP/1.x 200 OK
Content-Type: application/x-www-form-urlencoded
Content-Length: 37
Code: Select all
<?php // HTTP GET Script
$handle = fsockopen("localhost", 8080, $errno, $errstr, 30);
if (!$handle) {
echo "$errstr ($errno)<br />\n";
} else {
$request = "GET /~*temp/~1-194-39debee1-0 HTTP/1.1\r\n";
$request .= "Host: localhost\r\n";
$request .= "Connection: Close\r\n\r\n";
fwrite($handle, $request);
while (!feof($handle)) {
echo fgets($handle, 128);
}
fclose($handle);
}
?>Code: Select all
<?php //HTTP PUT script
$handle = fsockopen("localhost", 8080, $errno, $errstr, 30);
if (!$handle) {
echo "$errstr ($errno)<br />\n";
} else {
$fp = fopen("file.txt","r");
if($fp){
$fstat = fstat($fp);
$conent_length = $fstat['size'];
$request = "PUT /temp/my_test_tab HTTP/1.1\r\n";
$request .= "Host: localhost\r\n";
$request .= "Content-Length: $conent_length\r\n";
$request .= "Connection: Close\r\n\r\n";
echo $request;
fwrite($handle, $request);
stream_copy_to_stream($fp, $handle);
fclose($fp);
echo '<br /><b style="color:green"><pre>';
while (!feof($handle)) {
echo fgets($handle, 128);
}
echo '</pre></b>';
fclose($handle);
}
}
?>Code: Select all
$request .= "Connection: Close\r\n\r\n";
Code: Select all
$request .= "Keep-Alive: 300\r\n";
$request .= "Connection: keep-alive\r\n";
1. can any one please tell me what is going wrong?
2. What about the Connection: header do I have to set it to Close or keep-alive