Google atom entry XML with PHP


Google atom entry XML with PHP

从这种类型的XML中提取信息的正确方法是什么?我尝试了这个https://developers.google.com/apps-script/articles/XML_tutorial#index但它已经过时了,并且在 Java 中

<?xml version="1.0" encoding="utf-8"?>
<atom:entry xmlns:atom="http://www.w3.org/2005/Atom" xmlns:apps="http://schemas.google.com/apps/2006">
<apps:property name="enable" value="true" />
<apps:property name="subject" value="Out of office" />
<apps:property name="message" value="If it&#39;s urgent you can contact me on 555-5555." />
<apps:property name="contactsOnly" value="true" />
<apps:property name="domainOnly" value="false" />
<apps:property name="startDate" value="2011-06-20" />
<apps:property name="endDate" value="2011-06-23" />
</atom:entry>

还是这个?

   <atom:entry>
    <atom:id>https://apps-apis.google.com/a/feeds/emailsettings/2.0/example.com/venu/signature</atom:id>
    <atom:updated>2009-04-17T15:29:21.064Z</atom:updated>
    <atom:link rel='self' type='application/atom+xml' href='https://apps-apis.google.com/a/feeds/emailsettings/2.0/example.com/venu/signature'/>
    <atom:link rel='edit' type='application/atom+xml' href='https://apps-apis.google.com/a/feeds/emailsettings/2.0/example.com/venu/signature'/>
    <apps:property name='signature' value='Regards from Venu at the help desk'/>
   </atom:entry>

我可以通过在 GET 地址的末尾添加 ?alt=json 然后使用 json_decode($response,true); 进入数组来做到这一点,但必须有一种更简单、更少资源消耗的方法

我为签名部分(第二个 xml)找到了这个答案http://stackoverflow.com/questions/4043183/using-php-to-manage-gmail-mail-filter-xml-files/12245227#12245227但第一个仍然有问题

谢谢

已更新

您可以使用 simpleXML 浏览源,如下所示:

$url = "http://organic.com.au/news/atom.xml";
$xml = simplexml_load_file($url) or die("Error: Cannot create object");
echo "<pre>";
echo "number of entries: " . count($xml->entry) . "<br/>";
echo "latest entry: " . $xml->entry[count($xml->entry) - 1 ]->summary . "<br/>";
echo "access third entry: " . $xml->entry[2]->summary . "<br/><br/>";
htmlspecialchars(print_r($xml));    // map the whole file
echo "</pre>";

这是在 php 中读取 XML 提要的最简单方法。但是,SimpleXML 将整个文件读取到内存中,因此如果您的提要超过 50MB,您可能希望流式传输文件并分块读取。