PHP POST

2009-03-15 20:51:04

This script allows you to send a POST request without using CURL or any other framework. A GET works fine too, just don't specify any data.

The original function wasn't mine (although I've edited it since then), but it's not quite so easy to find this on the web, due to being flooded with other results. So here ya go...

Code: (PHP)
function post_request($url, $data, $cookie="") {
$params = array(
'http' => array(
'method' => 'POST',
'content' => $data,
'header' => "Cookie: $cookie\r\n"
)
);
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
if (!$fp) {
throw new Exception("Problem with $url");
}
$response = @stream_get_contents($fp);
if ($response === false) {
throw new Exception("Problem reading data from $url");
}
return $response;
}


Hope it helps ya