dongyaofu0599 2010-06-24 10:14
浏览 40
已采纳

xml解析问题php

I have a problem.

I have 2 php scripts, one which sends xml data using curl and one which is supposed to read the posted data.

The problem is the reciever script is not getting any of the elements in the xml.

Any help would be 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();
?>
  • 写回答

1条回答 默认 最新

  • douchui1657 2010-06-29 18:54
    关注

    I was unable to get DOMDocument to even load this XML string, with or without the namespace. I have no idea why.

    I'd suggest using SimpleXMLElement. I use it a lot, and it works great. I was also able to get it to parse your XML string.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分