views:

1075

answers:

3

I want to send a raw http packet to a webserver and recieve its response but i cant find out a way to do it. im inexperianced with sockets and every link i find uses sockets to send udp packets. any help would be great.

+5  A: 

Take a look at this simple example from the fsockopen manual page:

<?php
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
    $out = "GET / HTTP/1.1\r\n";
    $out .= "Host: www.example.com\r\n";
    $out .= "Connection: Close\r\n\r\n";
    fwrite($fp, $out);
    while (!feof($fp)) {
        echo fgets($fp, 128);
    }
    fclose($fp);
}
?>

The connection to the server is established with fsockpen. $out holds the HTTP request that’s then send with frwite. The HTTP response is then read with fgets.

Gumbo
its exactly what i needed. i think i need to rethink my approach to the packet spoofing tho :S
Ozzy
+2  A: 

If all you want to do is perform a GET request and receive the body of the response, most of the file functions support using urls:

<?php

$html = file_get_contents('http://google.com');

?>

<?php

$fh = fopen('http://google.com', 'r');
while (!feof($fh)) {
    $html .= fread($fh);
}
fclose($fh);

?>

For more than simple GETs, use curl (you have to compile it into php). With curl you can do POST and HEAD requests, as well as set various headers.

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://google.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$html = curl_exec($ch);

?>
Dana
+2  A: 

cURL is easier than implementing client side HTTP. All you have to do is set a few options and cURL handles the rest.

$curl = curl_init($URL);
curl_setopt_array($curl,
    array(
        CURLOPT_USERAGENT => 'Mozilla/5.0 (PLAYSTATION 3; 2.00)',
        CURLOPT_HTTPAUTH => CURLAUTH_ANY,
        CURLOPT_USERPWD => 'User:Password',
        CURLOPT_RETURNTRANSFER => True,
        CURLOPT_FOLLOWLOCATION => True
        // set CURLOPT_HEADER to True if you want headers in the result.
    )
);
$result = curl_exec($curl);

If you need to set a header that cURL doesn't support, use the CURLOPT_HTTPHEADER option, passing an array of additional headers. Set CURLOPT_HEADERFUNCTION to a callback if you need to parse headers. Read the docs for curl_setopt for more options.

outis