Friday 4th April 2008

by Sajith M.R

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:

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

Tags: , , , ,

· · · ◊ ◊ ◊ · · ·

8 Responses to “PHP post without curl”

  1. adam says:

    thank you

  2. Blog SEO says:

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

  3. Sajith M.R says:

    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 ?

  4. Farzanegan says:

    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!

  5. Johnny says:

    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.

  6. nerkn says:

    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.

  7. Sajith M.R says:

    Both are almost same , i think

  8. Binny V A says:

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

· · · ◊ ◊ ◊ · · ·

Leave a Reply

وظائف 2011 تحويل العملات برامج برنامج تسريع التحميل برنامج الفلاش برنامج محول الصوتيات عربي hotel 2011 زيادة رواتب العساكر 1431