This question already has an answer here:
When i am trying to hit the curl with xml headers as well as post fields. I am getting error as bad request. I don't know what's going wrong, can any one help with this?
Mobile field data passed in header is triple des converted
$url="www.test.com/api";
$xml = '<?xml version="1.0" encoding="UTF-8"?><a><b>hai</b></a>';
$xml_header = trim('<?xml version="1.0" encoding="UTF-8"?><mobile>4xKReHFU60Ova0aqHxZldg==</mobile>
') ;
$headers = array(
"Content-type: text/xml",
"Content-length: " .strlen($xml_header),
"Connection: close",
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// set url
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, array("xml"=>$xml));
// Check if any error occurred
if(!curl_errno($ch))
{
$info = curl_getinfo($ch);
echo "<pre>";var_dump($info);
echo 'error:' . curl_error($ch);
//echo 'Took ' . $info['total_time'] . ' seconds to send a request to ' . $info['url'];
}
$result = curl_exec($ch);
echo "<pre>";print_r($result);exit;
</div>
Bad Request is normally send from the server you interact with to tell you that there was a serious problem with the request, most often a malformed HTTP request up to that point that the remote server does not know at all what to do with it.
That is to say: You hit the jackpot of breaking it.
Remove bit after bit from your request until you get a more specific error message that can guide you more precisely when adding things again to not run into the same error again.
This is only general advice. The problem I could spot more concretely has been asked and answered already and I therefore closed it against a duplicate that should remind you, that having curl dealing with the content-length is most often easier as when you try to deal yourself with it (no pun intended).