tags:

views:

222

answers:

2

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

A: 

Try encoding your single quotes by replacing them with &apos; entities.

Or maybe the parser doesn't like you attributes being in single quotes, and only recognises double quotes, so try this instead:

<input name=\"id\">
cxfx
I changed them from single to double and have used the \" technique to no avail. Same for using entities and other such things. Thanks for the reply though
andy-score
+3  A: 

Try sending your XML data as normal POST string don't mention header. and before sending use $generated_xml = urlencode($generated_xml);.

and on the external server side use $generated_xml = urldecode($generated_xml); and parse the data.

Pragati Sureka
it seems just using `urlencode()` on my side works without having to have it decoded at the other end. I shall not question it.
andy-score