PHP post without curl
Posted on 04. Apr, 2008 by Sajith M.R in Downloads, php, php source code
You can simulate the post method using php without the help of curl library.
download full source code:
![]()
See the code below:
function do_post_request($url, $data, $optional_headers = null) {
$params = array('http' =>; array('method' =>; 'POST',
'content' =>; $data
));
if ($optional_headers !== null) {
$params['http']['header'] = $optional_headers;
}
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
if (!$fp) {
throw new Exception("Problem with $url, $php_errormsg");
}
$response = @stream_get_contents($fp);
if ($response === false) {
throw new Exception("Problem reading data from $url, $php_errormsg");
}
return $response;
}
?>
Download the full source code from post_without_curl.zip



Binny V A
Apr 4th, 2008
An easier way to do this – load() function. With and without curl
Sajith M.R
Apr 4th, 2008
Both are almost same , i think
nerkn
Mar 28th, 2009
Hi,
I’s very compact and working solution so a big thanx to you.
I thing arguments need explanation. Cause I need to unzip the file. For copy&pasters:
$data can be prepared with use of http_build_query. give assoc array. url ;I used http://domain.com/file.php rest resides in $data.
Mr Sajith Your function is not function a kind of library I guess. It could be complete for a project but for a small posts I like this one.
Johnny
Apr 14th, 2009
Hi,
I’m trying to use the above code and it keeps throwing the first exception.. do you know why, or how i can fix it ?
10x.
Farzanegan
Apr 23rd, 2009
Looks like it was stolen (w/out reference) from : http://netevil.org/blog/2006/nov/http-post-from-php-without-curl
(yes, 2006)
Plagarism FTW!
Sajith M.R
Apr 26th, 2009
Nice Farzanegan, i never mention anywhere that i did the code. It is for those who seeks for code. And moreover, i never made my own PHP from home. I got the php codes and documentation from php.net. Should I mention all these in my all project ?
Blog SEO
May 14th, 2009
I try to do the same, “why reinvent the wheel”
But you could have let a link to original source
adam
Dec 3rd, 2009
thank you