我是PHP新手,这可能是一个愚蠢的问题,所以不要因为我不理解某事而投票给我...
php -r "print_r(simplexml_load_file('http://twitter.com/statuses/user_timeline/31139114.rss'));"
Run Code Online (Sandbox Code Playgroud)
让我们以此为例,通过运行此命令,我得到(在屏幕上)XML输出.
我的问题是可以保存这些数据,而不仅仅是屏幕,但是在一个文件中,然后读取该文件并完全相同,就好像你已经使那个simplexml_load_file()
您可以使用以下内容下载数据file_get_contents; 它会在一个PHP字符串中为您提供整个XML.
例如 :
$xml = file_get_contents('http://twitter.com/statuses/user_timeline/31139114.rss');
Run Code Online (Sandbox Code Playgroud)
$xml 现在包含XML字符串.
然后,您可以使用将该字符串写入文件file_put_contents.
例如 :
file_put_contents('/home/squale/developpement/tests/temp/test.xml', $xml);
Run Code Online (Sandbox Code Playgroud)
并且,从命令行检查文件:
$ 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>
...
...
Run Code Online (Sandbox Code Playgroud)
之后,您可以使用simplexml_load_file该文件进行读取.
例如 :
$data = file_get_contents('/home/squale/developpement/tests/temp/test.xml');
Run Code Online (Sandbox Code Playgroud)
而$data现在包含的XML字符串;-)
考虑从远程服务器获取的XML是一个字符串,不需要序列化它; 并且file_put_contents更容易fopen+ fwrite+ fclose;-)