使用Zend_GData和Google Contacts API将联系人添加到群组

时间:2011-05-26 07:38:17

标签: php contact zend-gdata

我有一个使用zend_gdata的应用程序,并使用下面的代码创建联系人。

$doc  = new DOMDocument();
$doc->formatOutput = true;
$entry = $doc->createElement('atom:entry');
$entry->setAttributeNS('http://www.w3.org/2000/xmlns/' ,'xmlns:atom', 'http://www.w3.org/2005/Atom');
$entry->setAttributeNS('http://www.w3.org/2000/xmlns/' , 'xmlns:gd', 'http://schemas.google.com/g/2005');
$doc->appendChild($entry);

// add name element
$name = $doc->createElement('gd:name');
$entry->appendChild($name);

$fullName = $doc->createElement('gd:fullName', htmlentities($data->firstname . ' ' . $data->lastname));
$name->appendChild($fullName);

// insert entry
$entryResult = $gdata->insertEntry($doc->saveXML(), 'http://www.google.com/m8/feeds/contacts/default/full');

是否有可能将一个组添加到刚刚创建的联系人中?

2 个答案:

答案 0 :(得分:2)

我有一个大班,不能把它全部粘贴,你需要把它放在一起

步骤1)

获取所有组(http://raiyaraj.wordpress.com/2008/09/17/gmail-gdata-contacts-group-via-proxy/)并找到您的组的ID或创建它(如果它不存在,您可以使用zend框架)

第2步)

生成xml

// create new entry
        $doc  = new DOMDocument();
        $doc->formatOutput = true;
        $entry = $doc->createElement('atom:entry');
        $entry->setAttributeNS('http://www.w3.org/2000/xmlns/' , 'xmlns:atom', 'http://www.w3.org/2005/Atom');
        $entry->setAttributeNS('http://www.w3.org/2000/xmlns/' , 'xmlns:gd', 'http://schemas.google.com/g/2005');
        $entry->setAttributeNS('http://www.w3.org/2000/xmlns/' , 'xmlns:gContact', 'http://schemas.google.com/contact/2008');
        $doc->appendChild($entry);

...add various stuff....
    $name = $doc->createElement('gd:name');
            $entry->appendChild($name);
            $fullName = $doc->createElement('gd:fullName', $this->name);
            $name->appendChild($fullName);
.....

        $group = $doc->createElement('gContact:groupMembershipInfo');
        $group->setAttribute('deleted' ,'false');
        $group->setAttribute('href' ,'http://www.google.com/m8/feeds/groups/' .urlencode($this->email) . '/base/'.$this->group_id);
        $entry->appendChild($group);

第3步)

连接到gmail并执行查询

$service = $this->service;
// perform login and set protocol version to 3.0
$client = $service;
$gdata = new Zend_Gdata($client);
$gdata->setMajorProtocolVersion(3);
$entryResult = $gdata->insertEntry($this->getXML(), 'https://www.google.com/m8/feeds/contacts/default/full');

return $entryResult->getLink('edit');

请注意您返回编辑链接,以便在保存时可以更新联系人或检查修改

答案 1 :(得分:0)

是的,这是可能的。请参阅以下文档。

http://code.google.com/apis/contacts/docs/3.0/reference.html#groupMembershipInfo