首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >xml解析问题php

xml解析问题php
EN

Stack Overflow用户
提问于 2010-06-24 18:14:02
回答 1查看 211关注 0票数 0

我有个问题。

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

问题是接收者脚本没有获得xml中的任何元素。

任何帮助都会得到重视。

发件人脚本:

代码语言:javascript
复制
<?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;
?>

接收者脚本:

代码语言:javascript
复制
<?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();
?>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-06-30 02:54:21

我甚至无法让DOMDocument加载这个XML字符串,不管有没有名称空间。我不知道为什么。

我建议使用SimpleXMLElement。我经常使用它,它工作得很好。我还能够让它解析您的XML字符串。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3108996

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档