Tuesday, 12 July 2011

PHP: How to send a POST request with parameters

 

The following piece of PHP code shows how to send a POST request to a website passing some requests parameters. It may be useful if you needed to process the page that is normally requested using POST method e.g. form submission result page.

The request is similar to what your browser would send if you populated a form using POST method on a webpage.
// Create map with request parameters
$params = array ('surname' => 'Filip', 'lastname' => 'Czaja');

// Build Http query using params
$query = http_build_query ($params);

// Create Http context details
$contextData = array (
'method' => 'POST',
'header' => "Connection: close\r\n".
"Content-Length: ".strlen($query)."\r\n",
'content'=> $query );

// Create context resource for our request
$context = stream_context_create (array ( 'http' => $contextData ));

// Read page rendered as result of your POST request
$result = file_get_contents (
'http://www.sample-post-page.com', // page url
false,
$context);

// Server response is now stored in $result variable so you can process it

6 comments:

Patricia said...

Hi,

I would like to test this piece of code, but I am not sure what the page url parameter stands for ('http://www.sample-post-page.com').
Could you explain this to me please?

Thank you! :)

Filip Czaja said...

Patricia
Url points to the web resource (website, REST service, etc) that you want to send the HTTP POST request to.

Si said...

This is a neat bit of programming, but I use the php curl function to achieve the same result:

<?php

$url = 'http://www.sample-post-page.com';
$postData = array();
$postData['surname'] = 'Filip'
$postData['lastname'] = 'Czaja';
$postData['submit'] ='submit';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$result = curl_exec($ch);
curl_close($ch);

?>

Filip Czaja said...

Hi Si

Currently I prefer using HTTP Extension(which also uses CURL internally). I think it's the easiest/cleanest way to send Http requests from your PHP code.

The only problem is that you need to install it first as it is not bundled with PHP. Recently I described the installation process on CentOS:
http://fczaja.blogspot.com/2012/01/install-peclhttp-on-centos.html

Adam Parsons said...

Hey, I've came across this helpful little piece of code and it worked great.

The best part about it is that it doesn't need any special libs/packages to be installed. It just works!

Thanks again!

Dave said...

Hope someone can answer this: I am trying this script and cannot get it to post to the external server. I have the script in a verify page which verifies a captcha input and then I want to send the post values off to the external server. Everything seems to be fine but it does not seem to be going off to the server. I have:

is_valid) {
// What happens when the CAPTCHA was entered incorrectly
die ("The reCAPTCHA wasn't entered correctly. Go back and try it again.
" .
"(reCAPTCHA said: " . $resp->error . ")");
} else {
unset($_POST['recaptcha_challenge_field']);
unset($_POST['recaptcha_response_field']);
// Create map with request parameters
$params = $_POST;

// Build Http query using 'arams
$query = http_build_query ($params);

// Create Http context details
$contextData = array (
'method' => 'POST',
'header' => "Connection: close\r\n".
"Content-Length: ".strlen($query)."\r\n",
'content'=> $query );

// Create context resource for our request
$context = stream_context_create (array ( 'http' => $contextData ));
// Read page rendered as result of your POST request
$result = file_get_contents (
'https://external.server.com/ProspectWebForm', // page url
false,
$context);

// Server response is now stored in $result variable so you can process it
//echo $result;
}
?>

Am I missing something?