I don't think we do a very good job of evangelizing some of the nice things that the PHP streams layer does in the PHP manual, or even in general. At least, every time I search for the code snippet that allows you to do an HTTP POST request, I don't find it in the manual and resort to reading the source. (You can find it if you search for "HTTP wrapper" in the online documentation, but that's not really what you think you're searching for when you're looking).
So, here's an example of how to send a POST request with straight up PHP, no cURL:
<?php 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; }
$optional_headers is a string containing additional HTTP headers that you would like to send in your request.
PHP's HTTP wrapper will automatically fill out the Content-Length header based on the length of the $data that you pass in. It will also automatically set the Content-Type to application/x-www-form-urlencoded if you don't specify one in the $optional_headers.
I find this very handy; I don't need to code in redirection logic, HTTP auth handling, user agent setting and so on; they are handled for me by PHP.
You may also want to look into http_build_query() which is a convenience function that allows you to assemble query/post parameters from a PHP variable, applying appropriate escaping.
Kudos to Sara Golemon for both of these things. You can find more documentation on the HTTP wrapper options in the HTTP and HTTPS page in the PHP manual.
You could further simplify the code by file file_get_contents() rather then fopen, (get data) fclose.
Parse error: parse error, unexpected T_NEW in /home/web_dev/www/passgomedia.com/HOHO.php on line 17
??
thanks dude, that code really got me on to the right track, creating a XML-RPC compilant blog autopinger!
I'm stuck on PHP 4.3 so unfortunately I'm not able to get this working because stream_get_contents is PHP 5.
Is there an alternative I can use? I tried fpassthru() and it did not seem to work right.
A PHP4 alternative (if it exists) would be really nice!
I wrote a PHP 4 alternative which you can see here:
<a href="http://madhuri-dixit.110mb.com">Madhuri Dixit</a> likes this cool tutorial. Madhuri was previously using cURL. Now Dixit will do it with vanilla PHP
I don't understand what we have to put into $data, I tried some requests like:
$data = "name1=info1&name2=info2$name3=info3";
But it doesn't work... what do we have to do?
Hi,
Same as Jack, I'm not sure what to put in $data, is it just an array or http_build_query()?
Another question, will the function redirect the page to new url?
Hey Wez, are you the dr. Evil from http://www.hatebook.org?
Try this for the data:
$data = array ('username' => $username, 'first_name' => $first_name, 'last_name' = $last_name);
$data = http_build_query($data);
Hey...
Could someone please help me with HTTPS POST request?
What changes do I need to make to change a request from HTTP to HTTPS?
Hi,
I am using the script to try and interact with the Google Calendar API which requires the content type to be set as "application/atom+xml".
I have used the option headers parameter to try and do this, but the response coming back from Google is claiming the Content-Type is "application/x-www-form-urlencoded".
How should I be adding the information to the headers parameter correctly? I have tried using an array ($Headers = Array('Content-Type'=> 'application/atom+xml');) and also just entering the string "Content-Type: application/atom+xml" with no luck.
Any ideas where I could be going wrong?
