[英]php simplexml_load_file
我是PHP的新手,这可能是一个愚蠢的问题,所以不要因为我不了解某些内容而将我投下赞成票...
php -r "print_r(simplexml_load_file('http://twitter.com/statuses/user_timeline/31139114.rss'));"
让我们以此为例,通过运行此命令,我可以获取(在屏幕上)XML输出。
我的问题是,可以将这些数据而不是仅保存在屏幕上,而是保存在一个文件中,然后读取该文件,并且与创建了simplexml_load_file()完全相同
您可以使用file_get_contents
类的文件下载数据; 它会在一个PHP字符串中为您提供整个XML。
例如 :
$xml = file_get_contents('http://twitter.com/statuses/user_timeline/31139114.rss');
$xml
现在包含XML字符串。
然后,您可以使用file_put_contents
将该字符串写入文件。
例如 :
file_put_contents('/home/squale/developpement/tests/temp/test.xml', $xml);
并且,从命令行检查文件:
$ cat test.xml
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
<channel>
<title>Twitter / eBayDailyDeals</title>
<link>http://twitter.com/eBayDailyDeals</link>
<atom:link type="application/rss+xml" href="http://twitter.com/statuses/user_timeline/31139114.rss" rel="self"/>
<description>Twitter updates from eBay Daily Deals / eBayDailyDeals.</description>
<language>en-us</language>
<ttl>40</ttl>
<item>
...
...
之后,您可以使用simplexml_load_file
读取该文件。
例如 :
$data = file_get_contents('/home/squale/developpement/tests/temp/test.xml');
现在$data
包含您的XML字符串;-)
考虑到从远程服务器获取的XML是一个字符串,因此无需序列化; 和file_put_contents
fopen
+ fwrite
+ fclose
更容易;-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.