0

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()

1 Answer 1

6

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 now contains the XML string.


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> 
...
...


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');

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 that fopen+fwrite+fclose ;-)

Sign up to request clarification or add additional context in comments.

3 Comments

I'm constantly impressed by the quality and detail of your answers.
I actually tried and something didn't work for me, although the difference between this example and my actual work is I have to put together a string (XML) and call it against other's site using simplexml_load_file(), so that how I can XML content inside of my $var, but I still don't know how to save it, I've tried file_put_contents() but I couldn't get it to work:(
To save, you'd do file_put_contents('myfile.xml', $simpleXmlNode->asXML());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.