PHP - cURL (post by parameter)
<?php
//this url return result in xml format
$url = http://www.example.com/receiveRequest.php;
$parameter = "name=henry";
$response = curl_xml($url, $parameter);
/* ------- Response -------*/
//header for response result in xml format
header('Content-type: text/xml');
echo $response;
/* ------- CURL_XML -------*/
function curl_xml($_url, $_xmlRequest) {
$header = array("content-type: application/x-www-form-urlencoded");
// initialize curl handle
$c = curl_init();
// set url to post to
curl_setopt($c, CURLOPT_URL, $_url);
//include header as needed
curl_setopt($c, CURLOPT_HTTPHEADER, $header);
//not include the header in th output
curl_setopt($c, CURLOPT_HEADER, 0);
// set post method in request otherwise curl will do a GET request
curl_setopt($c, CURLOPT_POST, 1);
// add POST fields
curl_setopt($c, CURLOPT_POSTFIELDS, $_xmlRequest);
// return into a variable
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
// times out after 8s
curl_setopt($c, CURLOPT_TIMEOUT, 8);
// run the whole process
$xml_response = curl_exec($c);
curl_close($c);
return $xml_response;
}
/* ------------------------*/
?>
//this url return result in xml format
$url = http://www.example.com/receiveRequest.php;
$parameter = "name=henry";
$response = curl_xml($url, $parameter);
/* ------- Response -------*/
//header for response result in xml format
header('Content-type: text/xml');
echo $response;
/* ------- CURL_XML -------*/
function curl_xml($_url, $_xmlRequest) {
$header = array("content-type: application/x-www-form-urlencoded");
// initialize curl handle
$c = curl_init();
// set url to post to
curl_setopt($c, CURLOPT_URL, $_url);
//include header as needed
curl_setopt($c, CURLOPT_HTTPHEADER, $header);
//not include the header in th output
curl_setopt($c, CURLOPT_HEADER, 0);
// set post method in request otherwise curl will do a GET request
curl_setopt($c, CURLOPT_POST, 1);
// add POST fields
curl_setopt($c, CURLOPT_POSTFIELDS, $_xmlRequest);
// return into a variable
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
// times out after 8s
curl_setopt($c, CURLOPT_TIMEOUT, 8);
// run the whole process
$xml_response = curl_exec($c);
curl_close($c);
return $xml_response;
}
/* ------------------------*/
?>