Feb 6 2009

Here’s something I don’t do very often, but I’ve written a quick procedural script that could easily be turned into a class that will submit data to a HTTP POST “webservice”. It wouldn’t be a web service per se, rather than just a PHP script that accepts POST parameters, as opposed to an actual SOAP endpoint.

<?php
 
// Put the URL of your "POST endpoint"
$url = 'http://some/script/that/accepts/POST/input.php';
 
// Add your field pairs to this array...
$field_pairs = array(
	array("field1", "avalue"),
	array("field2", "bvalue"),
	);
 
// This bit prepares the fields by imploding them into a format similar to a query string...
$fields = array();
foreach($field_pairs as $field_pair)
{
	$fields[] = implode("=", $field_pair);
}
$postfields = implode("&", $fields);
 
// This sends the request through cURL
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, $postfields);
curl_exec($c);
curl_close($c);
 
?>

Leave a Reply