6

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.

Ozzy
  • 10,285
  • 26
  • 94
  • 138
  • @Ozzy: note - HTTP uses TCP (streams) not UDP (packets). – Stephen C Sep 22 '09 at 23:14
  • Why do you want to implement HTTP? There's a decent chance cURL will do what you want so you don't have to reinvent the wheel. – outis Sep 22 '09 at 23:21
  • @outis, im trying to authenticate my script as a ps3 on the psn network. im trying to emulate the raw packets that i captured using wireshark. but i keep getting "auth needed" or "request denied" responses :( – Ozzy Sep 22 '09 at 23:24
  • What type of authentication does PSN use? – outis Sep 23 '09 at 05:10
  • well PSN for websites just uses a complicated http sequence which i have already emulated. the PS3 auth for PSN is alot more complicated tho... i have no idea where to start – Ozzy Sep 23 '09 at 08:30

3 Answers3

9

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
  • 643,351
  • 109
  • 780
  • 844
  • 1
    its exactly what i needed. i think i need to rethink my approach to the packet spoofing tho :S – Ozzy Sep 22 '09 at 23:18
3

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
  • 1,048
  • 8
  • 10
  • any of that would be called a wrapper implementation ... Ozzy was looking for *raw* http. But having said that ... there is nothing raw about http ... depends from what standpoint its being looked upon! – ϹοδεMεδιϲ Jan 13 '11 at 02:35
2

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
  • 75,655
  • 22
  • 151
  • 221