Google API联系人v.3,PHP:向联系人添加联系人
问题描述:
我成功地使用cURL创建了一个新联系人,但是当我想为此联系人添加组成员时,出现400错误。我读this docs 并提出了相同的请求,但它没有奏效。 我在做什么错?感谢您的任何想法!Google API联系人v.3,PHP:向联系人添加联系人
这是我如何创建一个组信息的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();
这是我的卷毛:
$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,我已经想通了由我自己:)
1 )首先,要更新联系人,你应该使用PUT请求,而不是POST。
2)在你的XML不能使用“默认”(你会得到另一个错误),你应该使用完整的电子邮件地址:
$group = $doc->createElement('gContact:groupMembershipInfo');
$entry->appendChild($group);
$group->setAttribute('deleted', 'false');
$group->setAttribute('href', 'http://www.google.com/m8/feeds/groups/[email protected]/base/6');
3)您将获得400错误,如果你的避风港”指定名称空间gContact。对于入门标签,整个事情应该是这样的:
$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)最后,添加联系人到一个特定群体,你并不需要更新(因为我从文档想到),你可以在创建联系人时做到这一点(是的,现在看起来很明显)。如果您尝试更新联系人的组而不先创建它,则会收到400错误(Entry没有设置任何字段)。
希望这会帮助别人!
P.S.为了解决这些问题,我使用了Google的“OAuth 2.0 Playground”,非常有用! https://developers.google.com/oauthplayground/
非常感谢您的帮助!我在长时间搜寻为什么我有400个错误。 – Zooly 2017-04-20 13:02:49