xml解析问题php
问题描述:
我有一个问题。xml解析问题php
我有2个PHP脚本,一个使用curl发送xml数据,另一个应该读取发布的数据。
问题是接收器脚本没有获取任何XML中的元素。
任何帮助将appriciated。
SENDER SCRIPT:
<?php
$xml = '
<SMS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://api.xxxxxxxxxxxxxxx.co.uk/send/xxxxxxxxxxxxxxx.xsd">
<auth>
<user>yourusername</user>
<pass>yourpassword</pass>
</auth>
<originator>your_sender_name</originator>
<messages>
<msg id="1" gsm="440000000000">
<text>Please come for you appointment tomorrow morning at 12:45</text>
</msg>
<msg id="1" gsm="440000000000">
<text>Please come for you appointment tomorrow morning at 14:00</text>
</msg>
</messages>
</SMS>';
function sendMessages($xml) {
$curl = curl_init();
//$url = "https://sapi.xxxxxxxxxxxxxxx.co.uk/send/xml.php";
$url = "http://api.xxxxxxxxxxxxxxx.co.uk/send/xml.php";
$options = array(CURLOPT_URL => $url, CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_POSTFIELDS => $xml);
curl_setopt_array($curl, $options);
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
echo sendMessages($xml);
//echo $xml;
?>
RECIEVER SCRIPT:
<?php
function logResult() {
$postdata = file_get_contents("php://input");
$dom = new DOMDocument();
$dom->loadXML($postdata);
$xp = new domxpath($dom);
$messages = $xp->query("//SMS/auth");
//var_dump($messages);
foreach ($messages as $node) {
//var_dump($node);
return $node->getAttribute('user');
//$node->getAttribute('sentdate');
//$node->getAttribute('donedate');
}
}
echo logResult();
?>
答
我无法得到DOMDocument
甚至加载这个XML字符串,有或没有命名空间。我不知道为什么。
我建议使用SimpleXMLElement。我使用它很多,而且效果很好。我也能够解析你的XML字符串。
发件人XML在这里http://fixee.org/paste/k5i6qca/,有时候*很奇怪;( – 2010-06-24 10:16:20
@Stefan Gehrig:你的邮件没有显示出来,你可以重新发布 – 2010-06-24 10:25:34
@Kyle:I只是通过正确缩进行使源代码和XML更具可读性...我的一方还没有答案 – 2010-06-24 10:27:32