如何更新/删除Google Contacts API v3?在PHP中

时间:2018-12-12 19:53:14

标签: php google-api googlecontactsapi

我正在用PHP编程Google API。联系人API v3。我可以创建联系人,但不能对其进行编辑和删除。

这是我得到的数据。例如:

Array([0] => Array([name] => Sergio [email] => sergioales@yahoo.com [id] => http://www.google.com/m8/feeds/contacts/email%40gmail.com/base/4205b6d80df6ed10 [etag] =>“ RX0zfDVSLyt7I2A9XBJXFk4NQgw。”));

$XML='<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"
xmlns:gContact="http://schemas.google.com/contact/2008"
xmlns:gd="http://schemas.google.com/g/2005" xmlns:batch="http://schemas.google.com/gdata/batch"><entry gd:etag="'.$etag.'">
    <batch:id>update</batch:id>
    <batch:operation type="update"/>
    <category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/g/2008#contact"/>
    <id>http://www.google.com/m8/feeds/contacts/email%40gmail.com/base/4205b6d80df6ed10</id>
    <updated>'.date("Y-m-d").'T'.date("H:i").':00.000Z</updated>
    <category scheme="http://schemas.google.com/g/2005#kind"
      term="http://schemas.google.com/contact/2008#contact"/>
    <title type="text">'.$name.'</title>
    <gd:name>
      <gd:givenName>'.$name.'</gd:givenName>
      <gd:familyName></gd:familyName>
      <gd:fullName>'.$name.'</gd:fullName>
    </gd:name>
    <content type="text">Actualizado el '.date("d-m-Y H:i").' </content>
    <gd:email address="' . $email. '" rel="http://schemas.google.com/g/2005#home"/>
    <gd:phoneNumber rel="http://schemas.google.com/g/2005#mobile" primary="true">25151515151</gd:phoneNumber>
    </entry></feed>';

并使用curl

$headers = array('Host: www.google.com',
  'Gdata-version: 3.0',
  'Content-type: application/atom+xml',
  'If-Match: *',
  'Authorization: OAuth ' . $access_token);
  $contactQuery = 'https://www.google.com/m8/feeds/contacts/default/full/4205b6d80df6ed10';

 // echo $contactQuery;

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $contactQuery);
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $XML);
  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, 400);
  curl_setopt($ch, CURLOPT_FAILONERROR, true);
  $result = curl_exec($ch);

我发送的XML是否正确? 有什么建议吗?

谢谢

0 个答案:

没有答案