Google API Contacts v.3, PHP: add a contact to the group

笑着哭i 提交于 2019-12-03 16:23:02

OK, I've figured it out by myself:)
1) First of all, to update a contact you should use PUT request, not POST.
2) In your XML you can't use "default" (you'll get another error), you should use the full email address:

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

3) You will get 400 error if you haven't specified the namespace gContact. The whole thing for the entry tag should look like this:

$entry = $doc->createElement('entry');
$entry->setAttribute('xmlns', 'http://www.w3.org/2005/Atom');
$entry->setAttribute('xmlns:gd', 'http://schemas.google.com/g/2005');
$entry->setAttribute('xmlns:gContact', 'http://schemas.google.com/contact/2008');
$doc->appendChild($entry); 

4) Finally, to add a contact to a particular group, you don't need to update it (as I thought from the docs), you can do it while creating a contact (yes, now it seems obvious). If you try to update a contact's group without creating it first, you'll get 400 error (Entry does not have any fields set).
Hope this will help somebody!
P.S. To solve these problems I used "OAuth 2.0 Playground" by Google, very useful! https://developers.google.com/oauthplayground/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!