从URL读取XML POST数据

时间:2014-04-25 10:21:26

标签: php xml

我正在与第三方短信供应商合作,他们通过网址向我发送短信发送报告,如下所示:

http://www.mydomain.com/dlr.php <DeliveryReport><message id="024042313063119191" sentdate="2014/04/23 15:06:31" donedate="2014/04/23 15:06:35" status="DELIVERED" gsmerror="0" price="7.0" /></DeliveryReport>

我正在尝试读取dlr.php中的XML数据,如下所示:

<?php

// read raw POST data

$postData = file_get_contents("php://input");

 $dom = new DOMDocument();

 $dom->loadXML($postData);

 // create new XPath object for quering XML elements (nodes)

$xPath = new domxpath($dom);

// query “message” element

 $reports = $xPath->query("/DeliveryReport/message");

 // write out attributes of each “message” element

foreach ($reports as $node) {

  echo “<br>id: “ . $node->getAttribute('id');

  echo “<br>sent: “ . $node->getAttribute('sentdate');

  echo “<br>done: “ . $node->getAttribute('donedate');

  echo “<br>status: “ . $node->getAttribute('status');

  echo “<br>gsmerrorcode: “ . $node->getAttribute('gsmerrorcode');

}

?>

我收到此错误:

Warning: DOMDocument::loadXML(): Empty string supplied as input in dlr.php

任何帮助我如何正确阅读发布的数据。

谢谢,

1 个答案:

答案 0 :(得分:0)

您可以简单地使用此功能从XML获取数据

function getFeed($feed_url) 
{

        $content = file_get_contents($feed_url);
        $x = new SimpleXmlElement($content);
        foreach($x->channel->item as $entry) : ?>
        <?php
        $pdate = $entry->pubDate;
            $pdate = rtrim($pdate,' -500');
            $pdate = explode(', ',$pdate);
        ?>
        <div >
              <a href="<?php echo $entry->link; ?>" target="_blank">
            <span > <?php echo $entry->title;?></span></a> <?php echo $pdate[1]; ?>
        </div>
        <?php 
        endforeach; 
    }

    getFeed("// Your URL");