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.

5
  • @Ozzy: note - HTTP uses TCP (streams) not UDP (packets). Commented Sep 22, 2009 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. Commented Sep 22, 2009 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 :( Commented Sep 22, 2009 at 23:24
  • What type of authentication does PSN use? Commented Sep 23, 2009 at 5: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 Commented Sep 23, 2009 at 8:30

3 Answers 3

10

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.

Sign up to request clarification or add additional context in comments.

1 Comment

its exactly what i needed. i think i need to rethink my approach to the packet spoofing tho :S
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);

?>

1 Comment

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!
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.