使用 PHP 管理 GMail 邮件过滤器 XML 文件

发布于 2024-09-29 03:49:21 字数 3682 浏览 5 评论 0原文

由于 GMail 可以导入和导出邮件过滤器,因此我想使用 PHP 脚本管理从 GMail 导出的 XML 文件,因为有一些 搜索过滤器中字符数的已知问题

我在 PHP 中找到了 simplexml_load_file 函数,并对执行的导出执行了 var_dump() ,但我现在似乎无法访问生成的 XML 文件中的 apps 命名空间。

按照 PHP 手册中的各个页面,我创建了这个非常简单的脚本来读取 XML,以便我可以开始处理我已经创建的过滤器。不幸的是,它似乎缺少关键部分!

<pre><?php

if(file_exists("mailfilters.xml")) {
  $xml = simplexml_load_file("mailfilters.xml");
  $namespaces = $xml->getNamespaces(true);
  foreach ($namespaces as $prefix => $ns) {
      $xml->registerXPathNamespace($prefix, $ns);
  }
  var_dump($xml);
} else {
  die("Failed to open filter file");
}

?></pre>

这将返回此数据(摘录)

["entry"]=>
array(268) {
  [0]=>
  object(SimpleXMLElement)#3 (5) {
    ["category"]=>
    object(SimpleXMLElement)#271 (1) {
      ["@attributes"]=>
      array(1) {
        ["term"]=>
        string(6) "filter"
      }
    }
    ["title"]=>
    string(11) "Mail Filter"
    ["id"]=>
    string(45) "tag:mail.google.com,2008:filter:1284991916868"
    ["updated"]=>
    string(20) "2010-10-28T11:59:31Z"
    ["content"]=>
    object(SimpleXMLElement)#272 (0) {
    }
  }
  [1]=>
  object(SimpleXMLElement)#4 (5) {
    ["category"]=>
    object(SimpleXMLElement)#272 (1) {
      ["@attributes"]=>
      array(1) {
        ["term"]=>
        string(6) "filter"
      }
    }
    ["title"]=>
    string(11) "Mail Filter"
    ["id"]=>
    string(45) "tag:mail.google.com,2008:filter:1284991925003"
    ["updated"]=>
    string(20) "2010-10-28T11:59:31Z"
    ["content"]=>
    object(SimpleXMLElement)#271 (0) {
    }
  }

这是我今天下载的 XML 文件的摘录,用于创建上述输出:

<?xml version='1.0' encoding='UTF-8'?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:apps='http://schemas.google.com/apps/2006'>
    <title>Mail Filters</title>
<id>tag:mail.google.com,2008:filters:1284991916868,...,1287734777820</id>
<updated>2010-10-28T11:59:31Z</updated>
<author>
    <name>My Name</name>
    <email>[email protected]</email>
</author>
<entry>
    <category term='filter'></category>
    <title>Mail Filter</title>
    <id>tag:mail.google.com,2008:filter:1284991916868</id>
    <updated>2010-10-28T11:59:31Z</updated>
    <content></content>
    <apps:property name='from' value='[email protected]'/>
    <apps:property name='shouldArchive' value='true'/>
    <apps:property name='shouldTrash' value='true'/>
</entry>
<entry>
    <category term='filter'></category>
    <title>Mail Filter</title>
    <id>tag:mail.google.com,2008:filter:1284993579743</id>
    <updated>2010-10-28T11:59:31Z</updated>
    <content></content>
    <apps:property name='subject' value='Some Relevant Subject'/>
    <apps:property name='label' value='MyCoolLabel'/>
    <apps:property name='shouldArchive' value='true'/>
</entry>

其他“apps:property”动词包括:

<apps:property name='hasTheWord' value=''/>
<apps:property name='shouldAlwaysMarkAsImportant' value=''/>
<apps:property name='doesNotHaveTheWord' value=''/>

Since GMail made it possible to import and export the mail filters, I'd like to manage the XML files that are exported from GMail using a PHP script, as there are some known issues with the number of characters in the search filters.

I've found the simplexml_load_file function in PHP, and have performed var_dump() against the performed export, but I now don't seem to be able to access the apps namespace within the generated XML file.

Following various pages within the PHP manual, I created this very simple script to read the XML out so I can start to process out the filters I've already created. Unfortunately, it seems to be missing the key parts!

<pre><?php

if(file_exists("mailfilters.xml")) {
  $xml = simplexml_load_file("mailfilters.xml");
  $namespaces = $xml->getNamespaces(true);
  foreach ($namespaces as $prefix => $ns) {
      $xml->registerXPathNamespace($prefix, $ns);
  }
  var_dump($xml);
} else {
  die("Failed to open filter file");
}

?></pre>

This returns this data (extract)

["entry"]=>
array(268) {
  [0]=>
  object(SimpleXMLElement)#3 (5) {
    ["category"]=>
    object(SimpleXMLElement)#271 (1) {
      ["@attributes"]=>
      array(1) {
        ["term"]=>
        string(6) "filter"
      }
    }
    ["title"]=>
    string(11) "Mail Filter"
    ["id"]=>
    string(45) "tag:mail.google.com,2008:filter:1284991916868"
    ["updated"]=>
    string(20) "2010-10-28T11:59:31Z"
    ["content"]=>
    object(SimpleXMLElement)#272 (0) {
    }
  }
  [1]=>
  object(SimpleXMLElement)#4 (5) {
    ["category"]=>
    object(SimpleXMLElement)#272 (1) {
      ["@attributes"]=>
      array(1) {
        ["term"]=>
        string(6) "filter"
      }
    }
    ["title"]=>
    string(11) "Mail Filter"
    ["id"]=>
    string(45) "tag:mail.google.com,2008:filter:1284991925003"
    ["updated"]=>
    string(20) "2010-10-28T11:59:31Z"
    ["content"]=>
    object(SimpleXMLElement)#271 (0) {
    }
  }

Here's an extract from the XML file I have downloaded today, used to create the above output:

<?xml version='1.0' encoding='UTF-8'?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:apps='http://schemas.google.com/apps/2006'>
    <title>Mail Filters</title>
<id>tag:mail.google.com,2008:filters:1284991916868,...,1287734777820</id>
<updated>2010-10-28T11:59:31Z</updated>
<author>
    <name>My Name</name>
    <email>[email protected]</email>
</author>
<entry>
    <category term='filter'></category>
    <title>Mail Filter</title>
    <id>tag:mail.google.com,2008:filter:1284991916868</id>
    <updated>2010-10-28T11:59:31Z</updated>
    <content></content>
    <apps:property name='from' value='[email protected]'/>
    <apps:property name='shouldArchive' value='true'/>
    <apps:property name='shouldTrash' value='true'/>
</entry>
<entry>
    <category term='filter'></category>
    <title>Mail Filter</title>
    <id>tag:mail.google.com,2008:filter:1284993579743</id>
    <updated>2010-10-28T11:59:31Z</updated>
    <content></content>
    <apps:property name='subject' value='Some Relevant Subject'/>
    <apps:property name='label' value='MyCoolLabel'/>
    <apps:property name='shouldArchive' value='true'/>
</entry>

Other "apps:property" verbs include:

<apps:property name='hasTheWord' value=''/>
<apps:property name='shouldAlwaysMarkAsImportant' value=''/>
<apps:property name='doesNotHaveTheWord' value=''/>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

随心而道 2024-10-06 03:49:21

我最近也遇到了类似的“问题”。我试图从 Google Apps 获取电子邮件签名设置。这就是 Google 的 XML 响应的样子:

<?xml version='1.0' encoding='UTF-8'?>
<entry xmlns='http://www.w3.org/2005/Atom' xmlns:apps='http://schemas.google.com/apps/2006'>
<id>https://apps-apis.google.com/a/feeds/emailsettings/2.0/domain.com/user/signature</id>
<updated>2012-08-31T15:22:03.067Z</updated>
<link rel='self' type='application/atom+xml' href='https://apps-apis.google.com/a/feeds/emailsettings/2.0/domain.com/user/[email protected]'/>
<link rel='edit' type='application/atom+xml' href='https://apps-apis.google.com/a/feeds/emailsettings/2.0/domain.com/user/[email protected]'/>
<apps:property name='signature' value='Here goes the email signature...'/>
</entry>

我无法使用 SimpleXMLElement 从 apps:property 中获取属性。这就是我最终解决它的方法:

    // create SimpleXMLElement with XML input as string
    $xml = new SimpleXMLElement($feed);

    // get namespace from XML
    $namespaces = $xml->getNamespaces(true);

    // get children (using namespace) and attributes
    $appsProperty = $xml->children($namespaces['apps'])->attributes();

    // attributes are now stored in array $appsProperty and can be accessed
    echo "Name: ".$appsProperty['name'];
    echo "Signature: ".$appsProperty['value'];

I recently had a similar "problem". I was trying to get the email signature settings from Google Apps. This is what the XML Response from Google looks like:

<?xml version='1.0' encoding='UTF-8'?>
<entry xmlns='http://www.w3.org/2005/Atom' xmlns:apps='http://schemas.google.com/apps/2006'>
<id>https://apps-apis.google.com/a/feeds/emailsettings/2.0/domain.com/user/signature</id>
<updated>2012-08-31T15:22:03.067Z</updated>
<link rel='self' type='application/atom+xml' href='https://apps-apis.google.com/a/feeds/emailsettings/2.0/domain.com/user/[email protected]'/>
<link rel='edit' type='application/atom+xml' href='https://apps-apis.google.com/a/feeds/emailsettings/2.0/domain.com/user/[email protected]'/>
<apps:property name='signature' value='Here goes the email signature...'/>
</entry>

I was not able the get the attributes out of apps:property using the SimpleXMLElement. This is how i finally solved it:

    // create SimpleXMLElement with XML input as string
    $xml = new SimpleXMLElement($feed);

    // get namespace from XML
    $namespaces = $xml->getNamespaces(true);

    // get children (using namespace) and attributes
    $appsProperty = $xml->children($namespaces['apps'])->attributes();

    // attributes are now stored in array $appsProperty and can be accessed
    echo "Name: ".$appsProperty['name'];
    echo "Signature: ".$appsProperty['value'];
乖不如嘢 2024-10-06 03:49:21

乔恩,

满分,从 Facebook 链接这个,否则我永远不会看到它,而我昨天正是这样做的,为了谷歌服务!我认为使用 XPath 有点转移注意力,让我向您展示如何访问这些元素,希望它能为您提供足够的信息来完成您想要做的事情。

您使用 $xml->getNamespaces() 走在正确的轨道上 - 但不要在那里迭代,您需要在您想要的父元素上使用您想要的名称空间。首先,您只需要处理入口元素,因为这些是大多数,您不妨循环并检查是否有正确的元素:

foreach($xml as $tag) {
  if($tag->getName() == "entry") {
    // awesome, do Cool Stuff (TM) here
  }
}

现在,您在 $tag 变量中拥有该元素,并且您想要应用程序:命名空间的子元素。因此,请执行以下操作:

$tag->getChildren($namespaces['apps']);

当您迭代该集合时,您将看到所需的信息。

HTH,

洛娜

Jon,

Full marks for linking this from Facebook, I'd never have seen it otherwise and I was doing EXACTLY this yesterday, for a google service!! I think using XPath is a bit of a red herring, let me show you how to access the elements and hopefully it will give you enough information for what you want to do.

You're on the right tracks with $xml->getNamespaces() - but don't iterate there, you need to use the namespace you want on the parent element you want. Firstly, you need to work only on the entry elements, since these are the majority, you might as well loop and check if you have the right one:

foreach($xml as $tag) {
  if($tag->getName() == "entry") {
    // awesome, do Cool Stuff (TM) here
  }
}

Now, you have the element in the $tag variable, and you want the apps: namespaced child elements. So do this:

$tag->getChildren($namespaces['apps']);

When you iterate over that collection, you will see the info you wanted.

HTH,

Lorna

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文