Scenario: Sending XML, generated using php, via cURL to an external server for parsing.
Problem: The XML contains tag attributes which cause problems when being sent using cURL
Code:
$generated_xml =
-- NOTE: NOT THE SYNTAX USED IN THE CODE, SPLIT FOR EASE OF READING --
"<xconnect>
<report>
<id>contact_get</id>
<input name='email'><![CDATA[EMAIL_CAPTURED_FROM_INPUT]]></input>
<input name='id'></input>
</report>
</xconnect>";
$aCurlHeaders = array ("Content-Type: text/xml");
$hCurl = curl_init();
-- NOTE: HTTPHEADER OPTION FAILS (page returns 'no xml sent') --
curl_setopt($hCurl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($hCurl, CURLOPT_POST, true);
curl_setopt($hCurl, CURLOPT_CONNECTTIMEOUT, 120);
//curl_setopt($hCurl, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));
curl_setopt($hCurl, CURLOPT_URL, "SITE_URL");
curl_setopt($hCurl, CURLOPT_POSTFIELDS, "XMLDOC=$generated_xml");
$sResp = curl_exec($hCurl);
curl_close($hCurl);
if($sResp){ echo $sResp; }
Further details:
I don't know a huge amount about cURL
When sent to the page which parses the XML via cURL, it returns 'XML document failed parsing', however when using their testing form, which submits directly to the server via a POST form, it works fine and returns the correct data.
The problem is linked to the attributes of the <input>
tags, removing them allows the xml to parse on the server properly, but returns empty as the attributes are required to retrieve the data from the server.
I have no access to the parsing page, which dictates the XML, though have a contact there who may be able to change the coding to not require attributes, though I would have thought it would be possible to do this without making changes.
Questions:
Why does sending HTTPHEADER cause the parser to think the info sent isn't XML, is it to do with the way I'm sending the XML?
Is it possible to block the XML from parsing in the php (if that's part of the problem)
I've seen examples of using attributes in cURL before so what is it about this
Thanks:
Thanks