Wez Furlong

Browse archives
Conference Presentations
Subscribe. (circulation 766)
Comments. (circulation 1)

Search powered by Google

I am Wez Furlong, Director of Engineering at Message Systems. My team is responsible for the fastest MTA on Earth.

I'm also a PHP Core developer and OpenSource contributor, residing in Maryland with Juliette, Xander and Lily. (read more)

15th November 2006 @ 13:59 EDT

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.

by Wez Furlong in .
Post a comment
15th November 2006 @ 17:40 EDT

You could further simplify the code by file file_get_contents() rather then fopen, (get data) fclose.

27th June 2007 @ 18:02 EDT

Parse error: parse error, unexpected T_NEW in /home/web_dev/www/passgomedia.com/HOHO.php on line 17

??

by myspace.com/mrbunsen in .
13th September 2007 @ 19:22 EDT

thanks dude, that code really got me on to the right track, creating a XML-RPC compilant blog autopinger!

by coops.se in .
22nd September 2007 @ 19:59 EDT

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.

by Randy Whitaker in .
9th October 2007 @ 11:39 EDT

A PHP4 alternative (if it exists) would be really nice!

by Raam in .
5th December 2007 @ 11:15 EDT

I wrote a PHP 4 alternative which you can see here:

http://www.enyem.com/wiki/index.php...T_request_(PHP)

by Csaba in .
12th December 2007 @ 07:04 EDT

<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

by Madhuri Dixit in .
15th December 2007 @ 20:13 EDT

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?

by Jack Leblanc in .
19th January @ 07:50 EDT

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?

by Welly Lee in .
5th February @ 14:15 EDT

Hey Wez, are you the dr. Evil from http://www.hatebook.org?

by http://www.amorliber.com in .
21st March @ 23:24 EDT

Try this for the data:

$data = array ('username' => $username, 'first_name' => $first_name, 'last_name' = $last_name);

$data = http_build_query($data);

by spatical.com in .
21st April @ 03:34 EDT

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?

by Sumedh Inamdar in .
22nd July @ 22:19 EDT

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?

by Andrew in .
Post a comment