2

我已经成功地通过 Zend 框架和 PHP 将联系人添加到谷歌。我也希望能够通过 CURL 做到这一点。有没有人有关于如何做到这一点的好教程?

4

1 回答 1

4

我终于能够通过 CURL 和访问令牌做到这一点。首先,我想说OAuth Playground非常有用。执行此操作需要 2 个主要组件:首先,您需要正确格式化 XML。其次,您需要将访问令牌放入 CURL 实例的标头中。下面是我使用的代码,它工作得很好:

session_start();
$temp = json_decode($_SESSION['token'], true);
$access = $temp['access_token'];

$contactXML = '<?xml version="1.0" encoding="utf-8"?>
<atom:entry xmlns:atom="http://www.w3.org/2005/Atom" xmlns:gd="http://schemas.google.com/g/2005">
<atom:category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/contact/2008#contact"/>
<gd:name>
<gd:givenName>Jackie</gd:givenName>
<gd:fullName>Jackie Frost</gd:fullName>
<gd:familyName>Frost</gd:familyName>
</gd:name>
<gd:email rel="http://schemas.google.com/g/2005#home" address="jackfrost@gmail.com"/>
<gd:phoneNumber rel="http://schemas.google.com/g/2005#home" primary="true">1111111111</gd:phoneNumber>
</atom:entry>';

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

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

$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, $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);
curl_exec($ch);

我希望这对正在寻找此答案的其他人有所帮助。使用 Playground 将帮助您找到要使用的正确 URL 以及标头中所需的正确参数。

于 2013-01-28T22:09:21.637 回答