开发者

cURL sending XML incorrectly when using tag attributes

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>";

$aCurlHead开发者_运维百科ers = 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


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.


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\">
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜