Sockets - HTTP Communication problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
claws
Forum Commoner
Posts: 73
Joined: Tue Jun 19, 2007 10:54 am

Sockets - HTTP Communication problem

Post by claws »

Hey all,
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.
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
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:

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);
}
?>
This is the script I'm using to do HTTP PUT request. Problem is, when I run this script it just keeps on loading and hangs in there. I never got the response:

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);
    }
}
?>
I even tried replacing

Code: Select all

 
    $request .= "Connection: Close\r\n\r\n";
 
by this (as seen in firefox headers):

Code: Select all

 
    $request .= "Keep-Alive: 300\r\n";
    $request .= "Connection: keep-alive\r\n";
 
none worked.

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
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: Sockets - HTTP Communication problem

Post by Ziq »

It's strange, but your first code works on my server. Try to use HTTP GET request to remote host.

Code: Select all

 
<?
// HTTP GET Script
$handle = fsockopen("forums.devnetwork.net", 80, $errno, $errstr, 30);
if (!$handle) {
    echo "$errstr ($errno)<br />\n";
} else {
    $request = "GET /viewforum.php?f=1&st=0&sk=t&sd=d&start=50 HTTP/1.1\r\n";
    $request .= "Host: forums.devnetwork.net\r\n";
    $request .= "Connection: Close\r\n\r\n";
 
    fwrite($handle, $request);
    while (!feof($handle)) {
        echo fgets($handle, 128);
    }
    fclose($handle);
}
?>
 
Result?
Post Reply