使用标签属性时cURL错误地发送了XML [英] cURL sending XML incorrectly when using tag attributes

查看:79
本文介绍了使用标签属性时cURL错误地发送了XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

方案:
将使用php生成的XML通过cURL发送到外部服务器进行解析。



问题:
XML包含标记属性,这些标记属性在使用cURL发送时会引起问题



代码:

  $ generation_xml = 
-注意:代码中不使用语法,为了便于阅读而拆分-
< xconnect>
< report>
< id> contact_get< / id>
<输入名称='电子邮件'><![CDATA [EMAIL_CAPTURED_FROM_INPUT]] >< / input>
<输入名称='id'>< / input>
< / report>
< / xconnect>;

$ aCurlHeaders =数组(内容类型:文本/ xml);
$ hCurl = curl_init();

-注意:HTTPHEADER选项失败(页面返回未发送xml)-

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 = $ generation_xml);

$ sResp = curl_exec($ hCurl);

curl_close($ hCurl);

if($ sResp){echo $ sResp; }

更多详细信息:



我对cURL不太了解



当发送到通过cURL解析XML的页面时,它返回 XML文档解析失败 ,但是使用他们的测试表单(通过POST表单直接提交到服务器)时,它可以正常工作并返回正确的数据。



问题与属性相关联< input> 标记中的一个,删除它们可以使xml正确地在服务器上解析,但是由于需要属性才能从服务器检索数据,因此返回空。 / p>

我没有权限访问解析页面,该页面决定了XML,尽管在那里我可以更改编码以不需要属性,但是我可以



问题:



为什么发送HTTPHEADER会导致解析器认为发送的信息不是XML,是否与我发送XML的方式有关?



是否有可能阻止XML在php中进行解析(如果



我以前见过在cURL中使用属性的示例,这是怎么回事



感谢:



感谢

解决方案

尝试以常规的POST字符串发送XML数据,不要提及标题。
并且在发送之前使用 $ genic_xml = urlencode($ genic_xml);



并在外部服务器端使用 $ genic_xml = urldecode($ generation_xml);
并解析数据。


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

解决方案

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.

这篇关于使用标签属性时cURL错误地发送了XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆