php获取xml数据

作者:matrix 被围观: 1,689 次 发布时间:2013-10-27 分类:零零星星 | 6 条评论 »

这是一个创建于 3805 天前的主题,其中的信息可能已经有所发展或是发生改变。

调用某些api后返回的数据可能会是xml格式,这就需要提取相关数据。

如果了解正则匹配的话可以用preg_match()来提取,最好还是用php内置的专用函数来处理xml

 

代码:

$xml = new DOMDocument(); // 首先要建一个DOMDocument对象    
$xml->load('http://api.189.cn/EMP/shorturl/long2short?access_token=76327c4e405b725021640fd629bfc3511382853781284&app_id=120032470000032374&longurl=hhtjim.COM'); // 加载Xml文件     
$postDom = $xml->getElementsByTagName("shorturl")->item(0)->nodeValue;    
echo $postDom;  

 

说明:

第3行"shorturl"为读取的标签名,运行结果将显示http://189.io/ReRTnn

第2行是读取xml文件:


This XML file does not appear to have any style information associated with it. The document tree is shown below. <result> <res_code>0</res_code> <res_message>短地址生成成功。</res_message> <shorturl>http://189.io/ReRTnn</shorturl> </result>

参考:http://developer.51cto.com/art/200912/166247.htm


貌似这个代码也行

$xml = new DOMDocument();  
$xml->load('http://api.189.cn/EMP/shorturl/long2short?access_token=76327c4e405b725021640fd629bfc3511382853781284&app_id=120032470000032374&longurl=hhtjim.COM'); // 加载Xml文件     
foreach($xml->getElementsByTagName('shorturl') as $shorturl);
$value = $shorturl->firstChild->nodeValue;   
echo $value;  
其他文章:
本文固定链接:https://www.hhtjim.com/php-for-xml-data.html
matrix
本文章由 matrix 于2013年10月27日发布在零零星星分类下,目前没有通告,你可以至底部留下评论。
转载请注明:php获取xml数据-HHTjim'S 部落格
关键字:, ,

有6 条评论 »

  1. 佳佳君 佳佳君 2013-11-1 11:00:13 +0800#4

    This XML file does not appear to have any style information associated with it. The document tree is shown below.
    这个怎么也复制过来了....

  2. xilouqingzhu xilouqingzhu 2013-10-31 16:01:06 +0800#3

    同样不知道获取这个有啥实际意义

  3. Small Desert Small Desert 2013-10-28 20:47:32 +0800#2

    嗯,这个玩意让我想到了api的数据采取

  4. 免费部落 免费部落 2013-10-28 11:30:21 +0800#1

    获取这个有什么意义,博主应该讲一下!

    • Matrix Matrix Moderator 2013-10-28 12:35:43 +0800

      嗯,这可以提取某些api返回的数据

添加新评论 »

 🙈 😱 😂 😛 😭 😳 😀 😆 👿 😉 😯 😮 😕 😎 😐 😥 😡 😈 💡

插入图片

NOTICE: You should type some Chinese word (like “你好”) in your comment to pass the spam-check, thanks for your patience!