PHP post without curl

You can simulate the post method using php without the help of curl library.
download full source code:
OpenID Integration PHP

See the code below:

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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

9 Comments , , , ,

9 Responses to “PHP post without curl”

  1. Binny V A April 4, 2008 at 11:44 am #

    An easier way to do this – load() function. With and without curl

  2. Sajith M.R April 4, 2008 at 12:04 pm #

    Both are almost same , i think

  3. nerkn March 28, 2009 at 6:38 am #

    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.

  4. Johnny April 14, 2009 at 10:02 pm #

    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.

  5. Farzanegan April 23, 2009 at 1:14 am #

    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!

  6. Sajith M.R April 26, 2009 at 4:05 am #

    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 ?

  7. Blog SEO May 14, 2009 at 7:57 pm #

    I try to do the same, “why reinvent the wheel” :) But you could have let a link to original source ;)

  8. adam December 3, 2009 at 10:51 pm #

    thank you

  9. Dixon Michael November 29, 2011 at 4:21 pm #

    Please help me to authenticate zabbix monitoring url with any possible function……to avoiding repeated login…..

Leave a Reply

More in Downloads, php, php source code (79 of 118 articles)


I think you know the use of OpenID. Else go here and learn http://en.wikipedia.org/wiki/OpenID http://openid.net/what/ http://openiddirectory.com/ In a nutshell, ...