如何使用PHP删除单个Gmail联系人

时间:2017-03-31 09:52:25

标签: php google-contacts

我正在使用此脚本尝试从GMAIL中删除联系人,但无法删除。

我推荐:https://developers.google.com/glass/v1/reference/contacts/delete

我的剧本:

$id = '******'; 
$contact_mail = '********';
$contact_id = '************';

$contactXML = '<?xml version="1.0" encoding="utf-8"?>
<entry gd:etag="*">
<id>http://www.google.com/m8/feeds/contacts/'.$contact_mail.'/base/'.$contact_id.'</id>
<category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/contact/2008#contact"/>
<link rel="self" type="application/atom+xml" href="https://www.google.com/m8/feeds/contacts/'.$contact_mail.'/full/'.$contact_id.'"/>
<link rel="edit" type="application/atom+xml" href="https://www.google.com/m8/feeds/contacts/'.$contact_mail.'/full/'.$contact_id.'"/></entry>'; 

    $headers = array(
    'Host: www.google.com',
    'Gdata-version: 3.0',
    'Content-length: '.strlen($contactXML),
    'Content-type: application/atom+xml',
    'Authorization: OAuth '.$access_token
    );

    $contactQuery = 'https://www.google.com/m8/feeds/contacts/default/full/'.$contact_id.'';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $contactQuery );
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $contactXML);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_FAILONERROR, true);
    $result = curl_exec($ch);           
    print_r($result);

请找出我错的地方或指导我任何其他解决方案。

1 个答案:

答案 0 :(得分:1)

  1. 您的OAuth标头不正确。将Authorization: OAuth ...更改为Authorization: Bearer ...
  2. 删除请求不会占用内容。
  3. 您链接到的文档和您调用的API完全不同。您已链接到Glass API,但您正在调用Contacts API:https://developers.google.com/google-apps/contacts/v3
  4. 还有其他一些问题可能是错误的(尤其是如何获取OAuth令牌),但如果没有从服务器收到的实际错误消息,则无法明确诊断。