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

I managed to create a new contact with cURL, but when I want to add group membership for this contact, I get 400 errors. I read this document and made the same request, but it did not work. What am I doing wrong? Thanks for any ideas!

This is how I create XML with group information:

$doc = new DOMDocument('1.0', 'UTF-8'); $doc->formatOutput = true; $entry = $doc->createElement('entry'); $entry->setAttribute('gd:etag', $etag); $doc->appendChild($entry); $category = $doc->createElement('category'); $category->setAttribute('scheme', 'http://schemas.google.com/g/2005#kind'); $category->setAttribute('term', 'http://schemas.google.com/contact/2008#contact'); $entry->appendChild($category); $id = $doc->createElement('id', 'http://www.google.com/m8/feeds/contacts/default/base/'.$idgmail); $entry->appendChild($id); $updated = $doc->createElement('updated', $update_info); $entry->appendChild($updated); // Add group info (My Contacts) $group = $doc->createElement('gContact:groupMembershipInfo'); $entry->appendChild($group); $group->setAttribute('deleted', 'false'); $group->setAttribute('href', 'http://www.google.com/m8/feeds/groups/default/base/6'); // Add another group info $group = $doc->createElement('gContact:groupMembershipInfo'); $entry->appendChild($group); $group->setAttribute('deleted', 'false'); $group->setAttribute('href', 'http://www.google.com/m8/feeds/groups/default/base/'.$my_group_id); $group_info = $doc->saveXML(); 

And this is my cURL:

 $url = 'https://www.google.com/m8/feeds/contacts/default/full/'.$idgmail.'/'; $headers = array( 'Host: www.google.com', 'Gdata-version: 3.0', 'Content-length: '.strlen($group_info), 'Content-type: application/atom+xml', 'If-Match: *', 'Authorization: OAuth '.$access, ); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $group_info); curl_setopt($curl, CURLOPT_FAILONERROR, true); $resp = curl_exec($curl); print_r($resp); // Prints nothing echo curl_getinfo($curl, CURLINFO_HTTP_CODE); // Gives 400 curl_close($curl); 
+4
source share
1 answer

Ok, I figured it out myself :)
1) First of all, to update a contact, you should use a PUT request, not a POST request.
2) In your XML you cannot use "default" (you will get another error), you must 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 a 400 error if you did not specify the gContact namespace. The whole thing for a post 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 specific group, you do not need to update it (as I thought from the documents), you can do this when creating a contact (yes, now it seems obvious). If you try to update a contact group without first creating it, you will get 400 errors (there are no fields set in the record).
Hope this helps someone! Postscript To solve these problems, I used the Google "OAuth 2.0 Playground", very useful! https://developers.google.com/oauthplayground/

+8
source

All Articles