2013-03-22 168 views
4

我成功地使用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); 

回答

8

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/

+0

非常感謝您的幫助!我在長時間搜尋爲什麼我有400個錯誤。 – Zooly 2017-04-20 13:02:49