By using this site you agree to the use of cookies by Brugbart and our partners.

Learn more

Making POST Requests With PHP

Tutorial on how to make post requests in PHP using build-in capabilities .

Edited: 2016-01-03 18:06

Last: Making HTTP GET Requests

Like in other languages, there are often many ways to do the same thing. This is just a couple of ways to perform POST requests with PHP – if you don't like these methods, more may be listed in the PHP Tutorial.

The first method to perform HTTP post requests with PHP that we are going to demonstrate, is using file_get_contents, the other will be using fread in combination with a couple of other functions – they are both making use of the stream_context_create function to fill out the required request header fields.

Code Explanation

The $sPD variable contains the data that you want to post, this variable should be formatted as a HTTP query string – some special characters therefor need to be encoded.

In both the file_get_contents and the fread function, you will now have two new parameters. The first is the use_include_path, since we are performing a HTTP Request, this is set to false in both examples – it would search for a file in the include_path if we where reading local files, and if set to true.

The second is the context parameter, this will be filled out with the return value of stream_context_create, which accepts the value of the $aHTTP array.

Using File_get_contents to Make POST Requests

To use file_get_contents to make POST requests, we need to use stream_context_create to manually fill out the header fields, and specify which wrapper is to be used – in this case HTTP.

$sURL = "http://brugbart.com/Examples/http-post.php"; // The POST URL
$sPD = "name=Jacob&bench=150"; // The POST Data
$aHTTP = array(
  'http' => // The wrapper to be used
    array(
    'method'  => 'POST', // Request Method
    // Request Headers Below
    'header'  => 'Content-type: application/x-www-form-urlencoded',
    'content' => $sPD
  )
);
$context = stream_context_create($aHTTP);
$contents = file_get_contents($sURL, false, $context);

echo $contents;

Using fread to Perform HTTP POST Requests

It is also possible to use the PHP fread function to perform HTTP POST Requests with. If you have followed the examples from earlier PHP Tutorials, it shouldn't be hard to figure out how this is done, as the code to fetch the file content is almost the same – the only difference is that stream_context_create is used to construct the required HTTP request headers.

$sURL = "http://brugbart.com/Examples/http-post.php"; // The POST URL
$sPD = "name=Jacob&bench=150"; // The POST Data
$aHTTP = array(
  'http' => // The wrapper to be used
    array(
    'method'  => 'POST', // Request Method
    // Request Headers Below
    'header'  => 'Content-type: application/x-www-form-urlencoded',
    'content' => $sPD
  )
);
$context = stream_context_create($aHTTP);
$handle = fopen($sURL, 'r', false, $context);
$contents = '';
while (!feof($handle)) {
  $contents .= fread($handle, 8192);
}
fclose($handle);

echo $contents;