使用PHP编程创建Google联系人时如何存入「联系人」标签而非「其他联系人」
解决方案
问题原因
创建的联系人默认未关联Google联系人体系内的「我的联系人」系统分组,因此会被自动归类到「其他联系人」分组,该分组默认不会参与多端同步。
修复步骤
1. 补充命名空间与分组关联字段
你需要在XML的atom:entry标签中新增gContact专属命名空间,同时添加groupMembershipInfo节点绑定默认的「我的联系人」分组即可。
2. 调整后的核心代码
// 新增gContact命名空间,添加groupMembershipInfo节点绑定我的联系人分组 $contactXML = '<?xml version="1.0" encoding="utf-8"?> ' . '<atom:entry xmlns:atom="http://www.w3.org/2005/Atom" xmlns:gd="http://schemas.google.com/g/2005" xmlns:gContact="http://schemas.google.com/contact/2008">' . ' <atom:category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/contact/2008#contact"/> ' . // 新增的分组绑定节点,固定href对应系统默认「我的联系人」分组 '<gContact:groupMembershipInfo deleted="false" href="http://www.google.com/m8/feeds/groups/default/base/6"/>'. '<gd:name> <gd:givenName>' . $id1 . '</gd:givenName> <gd:fullName></gd:fullName> <gd:familyName>' . $id3 . '</gd:familyName>' . ' </gd:name> <gd:email rel="http://schemas.google.com/g/2005#home" address="' . $id2 . '"/> ' . '<gd:im address="'. $id2 .'" protocol="http://schemas.google.com/g/2005#GOOGLE_TALK" primary="true" rel="http://schemas.google.com/g/2005#home"/>' . ' <gd:phoneNumber rel="http://schemas.google.com/g/2005#home" primary="true">' . $id4 . '</gd:phoneNumber> </atom:entry>'; $headers = array('Host: www.google.com','Gdata-version: 3.0','Content-length: ' . strlen($contactXML),'Content-type: application/atom+xml','Authorization: OAuth ' . $accesstoken); $contactQuery = 'https://www.google.com/m8/feeds/contacts/default/full/'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $contactQuery); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $contactXML); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($ch, CURLOPT_TIMEOUT, 400); curl_setopt($ch, CURLOPT_FAILONERROR, true); $result = curl_exec($ch); $xml = simplexml_load_string($result);
3. 验证效果
修改后调用接口创建的联系人会直接出现在「联系人」标签页下,无需手动移动,支持多端同步。
内容的提问来源于stack exchange,提问作者ashish rhatwal
相关产品推荐
相关产品推荐

