简体   繁体   English

Google API Contacts v.3,PHP:向群组添加联系人

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

I successfully managed to create a new contact with cURL, but when I want to add group membership for this contact, I get 400 error. 我成功地设法创建了一个与cURL的新联系人,但是当我想为此联系人添加组成员资格时,我收到了400错误。 I've read this docs and made the same request, but it didn't work. 我已阅读此文档并提出相同的请求,但它无效。 What am I doing wrong? 我究竟做错了什么? Thanks for any ideas! 谢谢你的任何想法!

This is how I create an XML with group information: 这就是我用组信息创建XML的方法:

$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: 这是我的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);

OK, I've figured it out by myself:) 好的,我自己想通了:)
1) First of all, to update a contact you should use PUT request, not POST. 1)首先,要更新联系人,您应该使用PUT请求,而不是POST。
2) In your XML you can't use "default" (you'll get another error), you should use the full email address: 2)在您的XML中,您不能使用“默认”(您将收到另一个错误),您应该使用完整的电子邮件地址:

$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. 3)如果未指定命名空间gContact,则会收到400错误。 The whole thing for the entry tag should look like this: entry标记的全部内容应如下所示:

$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). 4)最后,要向特定组添加联系人,您不需要更新它(正如我从文档中所想的那样),您可以在创建联系人时执行此操作(是的,现在看来很明显)。 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). 如果您尝试更新联系人的组而不先创建它,则会收到400错误(条目没有设置任何字段)。
Hope this will help somebody! 希望这会对某人有所帮助!
PS To solve these problems I used "OAuth 2.0 Playground" by Google, very useful! PS为了解决这些问题,我使用了Google的“OAuth 2.0 Playground”,非常实用! https://developers.google.com/oauthplayground/ https://developers.google.com/oauthplayground/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM