dssqq82402 2016-03-16 15:23
浏览 23

使用PHP的Google原子入口XML

What is the proper way to extract information from this type of XML ? I tried this https://developers.google.com/apps-script/articles/XML_tutorial#index but it's outdated and in 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>

or this one ?

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

I can do it by adding ?alt=json at the end of the GET adress and then using json_decode($response,true); to turn in in to array, but there must be a more easy and less resource consuming way

I found this answer for the signature part (second xml) http://stackoverflow.com/questions/4043183/using-php-to-manage-gmail-mail-filter-xml-files/12245227#12245227 but still having problems for the first one

Thanks

  • 写回答

1条回答 默认 最新

  • douqiao4450 2016-03-16 15:40
    关注

    UPDATED

    You can go through the feed using simpleXML like this:

    $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>";
    

    Thats the easiest way to read XML feeds in php. However, SimpleXML reads the whole file to memory, so if you have feeds that are above 50MB you might ewant to stream the file and read it in chunks.

    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助