php simplexml_load_file [英] php simplexml_load_file
问题描述
我是 PHP 新手,这可能是一个愚蠢的问题,所以不要因为我不明白某些东西就投票给我...
I'm new to PHP and this may be a stupid question to ask, so don't vote me down just because I don't understand something...
php -r "print_r(simplexml_load_file('http://twitter.com/statuses/user_timeline/31139114.rss'));"
让我们以这个为例,通过运行这个命令,我得到(在屏幕上)XML 输出.
let's take this for an example, by running this command i get (on screen) XML output.
我的问题是,是否可以将这些数据保存在一个文件中,而不仅仅是屏幕,而是保存在一个文件中,然后读取该文件,并且与您制作的 simplexml_load_file() 完全相同
my question is it possible to save this data instead of just screen but in a file and then read that file and have exact same as if you've have made that simplexml_load_file()
推荐答案
您可以下载数据,使用类似 file_get_contents
;它会在单个 PHP 字符串中为您提供整个 XML.
例如:
You can download the data, using something like file_get_contents
; it'll get you the whole XML in a single PHP string.
For instance :
$xml = file_get_contents('http://twitter.com/statuses/user_timeline/31139114.rss');
$xml
现在包含 XML 字符串.
$xml
now contains the XML string.
然后,您可以使用 file_put_contents
将该字符串写入文件.
例如:
Then, you can write that string to a file, using file_put_contents
.
For instance :
file_put_contents('/home/squale/developpement/tests/temp/test.xml', $xml);
并且,从命令行检查文件:
And, to check the file, from the command-line :
$ 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
从该文件中读取.
例如:
After that, you can use simplexml_load_file
to read from that file.
For instance :
$data = file_get_contents('/home/squale/developpement/tests/temp/test.xml');
并且 $data
现在包含您的 XML 字符串 ;-)
And $data
now contains your XML string ;-)
考虑到你从远程服务器得到的 XML 是一个字符串,不需要序列化它;并且 file_put_contents
比 fopen
+fwrite
+fclose
更容易;-)
Considering the XML you get from the remote server is a string, no need to serialize it ; and file_put_contents
is easier that fopen
+fwrite
+fclose
;-)
这篇关于php simplexml_load_file的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!