xml解析问题php

时间:2010-06-24 10:14:02

标签: php xml

我有一个问题。

我有2个php脚本,一个使用curl发送xml数据,另一个用于读取发布的数据。

问题是收件人脚本没有获取xml中的任何元素。

任何帮助都会得到满足。

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();
?>

1 个答案:

答案 0 :(得分:0)

无论是否加载名称空间,我都无法让DOMDocument加载此XML字符串。我不明白为什么。

我建议使用SimpleXMLElement。我经常使用它,效果很好。我还能够解析你的XML字符串。