I'm trying to get response from API - I have to send an xml document, and then I should get another xml as an response, but all I got is 'xml error no element found'....
Here is my cURL code in php:
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'email=' . $email . '&password=' . $password . '&content=' . $xml);
$ex = curl_exec($ch);
Unfortunatelly, when I'm trying to echo $ex - the above error is shown. All of my variables are correct, so does my xml (document is given in $xml variable).
What am I missing?
You are setting Content-Type header to text/xml, but actually you are trying to send the data as application/x-www-form-urlencoded. Either correct the header, or only send the $xml variable.
For example:
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: text/xml',
'Content-Length: '. strlen($xml))
);
$result = curl_exec($ch);
The CURLOPT_RETURNTRANSFER
is purely so that the response from the remote server gets placed in $result
rather than echoed.