Aaron Saray

open source programmer,
web developer

entrepreneur, author
and musician

My Blog

contains PHP, Web and business/entrepreneurial related content. Please join in the conversation!

Posting Requests in PHP without CURL

Can it be done? YES! Luckily, functions like file_get_contents() support stream contexts.

In this example, I want to post to my form my login credentials of username “aaron” and password “chicken”. This will be posting to the URL of http://test.com/login.do.php. I’ll show the code first, and then lets talk about what it does.

1
2
3
4
5
6
7
8
9
10
$args = array ('username'=>'aaron', 'password'=>'chicken');
$uri = 'http://test.com/login.do.php';

$opts = array('http'=>array('method'=>'POST', 'header'=>'Content-Type: application/x-www-form-urlencoded', 'content'=>http_build_query($args)));

$context = stream_context_create($opts);

$result = file_get_contents($uri, false, $context);

print $result;

Lets disect line by line:

The first line dealing with $args is setting up our post parameters. The next line is our uri target. If this was an HTML form, it might look like this:

1
2
3
4
5
<form action="http://test.com/login.do.php" method="POST">
Username: <input name="username" /><br />
Password: <input name="password" /><br />
<input type="submit" />
</form>

The next line is the $opts array. This will be the options that we send to the stream context. The array is keyed by the type of stream we’re creating here – in this case ‘http’. This points to an array of options. First, the method of the request, in this case “POST”. Next, the header that must be sent in order to submit the request. For the most part, your browser handles sending this – but we have to specify it here. It simply is keying the request to let it know that its a form submission. The final key is the content key – which is what is submitted in a typical request below the headers. Here we’re using PHP’s http_build_query() to save ourselves some time.

Moving on, we create a new context using stream_context_create(), assign that to $context using $opts as our parameter. Think of $context not as a value, but as a handle – similar to an fopen or other resource handle.

Finally, we retrieve the contents of our request using file_get_contents(). Do note: you must have fopen_url = true in your php.ini. This allows us to retrieve content via an external URL. We pass the location of our post request, false because we don’t want send any additional flags, and a pointer to our stream context.

After this is complete, we should have the output in the $result variable.

This entry was posted in PHP and tagged . Bookmark the permalink.

2 Responses to Posting Requests in PHP without CURL

  1. Holy Cow! That is too useful, assuming of course, that you have allow_url_fopen=On. Still, it’s the little things that can save your butt. This one definitely goes into the ol’ bag o’ tricks.

  2. I agree with Sjan, this is a great post Aaron, thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>