php simplexml_加载_文件

php simplexml_加载_文件,php,xml,simplexml,Php,Xml,Simplexml,我是PHP新手,这可能是一个愚蠢的问题,所以不要因为我不懂某些东西就投票否决我 php -r "print_r(simplexml_load_file('http://twitter.com/statuses/user_timeline/31139114.rss'));" 让我们以此为例,通过运行此命令,我可以(在屏幕上)获得XML输出 我的问题是,是否可以将这些数据保存在一个文件中,而不仅仅是屏幕上的数据,然后读取该文件,并将其与使用simplexml加载文件()完全相同?您可以使用以下方式

我是PHP新手,这可能是一个愚蠢的问题,所以不要因为我不懂某些东西就投票否决我

php -r "print_r(simplexml_load_file('http://twitter.com/statuses/user_timeline/31139114.rss'));"
让我们以此为例,通过运行此命令,我可以(在屏幕上)获得XML输出


我的问题是,是否可以将这些数据保存在一个文件中,而不仅仅是屏幕上的数据,然后读取该文件,并将其与使用simplexml加载文件()完全相同?您可以使用以下方式下载数据:;它将在一个PHP字符串中获得整个XML。
例如:

$xml = file_get_contents('http://twitter.com/statuses/user_timeline/31139114.rss');
file_put_contents('/home/squale/developpement/tests/temp/test.xml', $xml);
$data = file_get_contents('/home/squale/developpement/tests/temp/test.xml');
$xml
现在包含xml字符串


然后,您可以使用将该字符串写入文件。
例如:

$xml = file_get_contents('http://twitter.com/statuses/user_timeline/31139114.rss');
file_put_contents('/home/squale/developpement/tests/temp/test.xml', $xml);
$data = file_get_contents('/home/squale/developpement/tests/temp/test.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> 
...
...
现在,
$data
包含您的XML字符串;-)



考虑到从远程服务器获得的XML是一个字符串,不需要序列化它;而且
file\u put\u contents
fopen
+
fwrite
+
fclose
;-)更简单

您可以使用以下方式下载数据:;它将在一个PHP字符串中获得整个XML。
例如:

$xml = file_get_contents('http://twitter.com/statuses/user_timeline/31139114.rss');
file_put_contents('/home/squale/developpement/tests/temp/test.xml', $xml);
$data = file_get_contents('/home/squale/developpement/tests/temp/test.xml');
$xml
现在包含xml字符串


然后,您可以使用将该字符串写入文件。
例如:

$xml = file_get_contents('http://twitter.com/statuses/user_timeline/31139114.rss');
file_put_contents('/home/squale/developpement/tests/temp/test.xml', $xml);
$data = file_get_contents('/home/squale/developpement/tests/temp/test.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> 
...
...
现在,
$data
包含您的XML字符串;-)



考虑到从远程服务器获得的XML是一个字符串,不需要序列化它;而且
file\u put\u contents
fopen
+
fwrite
+
fclose
;-)更简单

你的答案的质量和细节给我留下了深刻的印象。我确实尝试过,但有些东西对我不起作用,尽管这个例子和我的实际工作之间的区别是我必须组合一个字符串(XML)并使用simplexml\u load\u file()对其他网站调用它,这样我就可以在$var中调用XML内容,但我仍然不知道如何保存它,我尝试了file_put_contents(),但无法使其工作:(要保存,您需要执行file_put_contents('myfile.xml',$simpleXmlNode->asXML());你的答案的质量和细节给我留下了深刻的印象。我确实尝试过,但有些东西对我不起作用,尽管这个例子和我的实际工作之间的区别是我必须组合一个字符串(XML),并使用simplexml_load_file()对其他网站进行调用,这样我就可以在$var中保存XML内容,但我仍然不知道如何保存它,我尝试了file_put_contents(),但无法使其工作:(要保存,您需要执行file_put_contents('myfile.XML',$simpleXmlNode->asXML());