PHP - HTTP Post
function curl_xml($_url, $_xmlRequest) {
$header = array("content-type: text/xml");
$c = curl_init();// initialize curl handle
curl_setopt($c, CURLOPT_URL, $_url);// set url to post to
curl_setopt($c, CURLOPT_HTTPHEADER, $header);//include header as needed
curl_setopt($c, CURLOPT_HEADER, 0);//not include the header in th output
curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1); //set follow redirect send by the server
curl_setopt($c, CURLOPT_MAXREDIR, 5); // Limit redirections to four
curl_setopt($c, CURLOPT_POST, 1);// set post method in request otherwise curl will do a GET request
curl_setopt($c, CURLOPT_POSTFIELDS, $_xmlRequest);// add POST fields
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);// return into a variable
curl_setopt($c, CURLOPT_TIMEOUT, 8); // times out after 8s
$xml_response = curl_exec($c);// run the whole process
curl_close($c);
return $xml_response;
}
The most important is to set CURLOPT_RETURNTRANSFER, because by default in PHP curl only sends data to server and do not wait to get response, it sometimes useful to only send request and do not wait for response.
$header = array("content-type: text/xml");
$c = curl_init();// initialize curl handle
curl_setopt($c, CURLOPT_URL, $_url);// set url to post to
curl_setopt($c, CURLOPT_HTTPHEADER, $header);//include header as needed
curl_setopt($c, CURLOPT_HEADER, 0);//not include the header in th output
curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1); //set follow redirect send by the server
curl_setopt($c, CURLOPT_MAXREDIR, 5); // Limit redirections to four
curl_setopt($c, CURLOPT_POST, 1);// set post method in request otherwise curl will do a GET request
curl_setopt($c, CURLOPT_POSTFIELDS, $_xmlRequest);// add POST fields
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);// return into a variable
curl_setopt($c, CURLOPT_TIMEOUT, 8); // times out after 8s
$xml_response = curl_exec($c);// run the whole process
curl_close($c);
return $xml_response;
}
The most important is to set CURLOPT_RETURNTRANSFER, because by default in PHP curl only sends data to server and do not wait to get response, it sometimes useful to only send request and do not wait for response.