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'));"
let's take this for an example, by running this command i get (on screen) XML output.
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()
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
now contains the XML string.Then, you can write that string to a file, using
file_put_contents
.For instance :
And, to check the file, from the command-line :
After that, you can use
simplexml_load_file
to read from that file.For instance :
And
$data
now contains your XML string ;-)Considering the XML you get from the remote server is a string, no need to serialize it ; and
file_put_contents
is easier thatfopen
+fwrite
+fclose
;-)