2013-01-24 102 views
1

我已经成功地通过Zend Framework和PHP添加了一个联系人到Google。我希望能够通过CURL来做到这一点。有没有人有一个很好的教程如何做到这一点?通过PHP添加Google联系人CURL

+0

我用google搜索“google contact curl”并得到[this](http://salmanzg.wordpress.com/2010/12/29/google-contact-data-api-via-curl/),这看起来很彻底。 ..你问的关于命令行卷曲或PHP的卷曲功能? – glomad

+0

我在问PHP的Curl函数,并且我搜索了一大堆没有运气的。感谢您的链接,我也会通读它。 – thomas

+0

关于堆栈溢出问题的书籍,工具,软件库,教程或其他非本地资源的建议是[off-topic](https://stackoverflow.com/help/on-topic),并且此问题可能会被关闭。使用谷歌的搜索引擎。当您开始实施解决方案并遇到特定问题时,您可以随时在此寻求帮助。 (只要你遵循[提问的指导原则](https://stackoverflow.com/help/asking),当然) –

回答

3

我终于可以通过CURL和访问令牌来做到这一点。首先,我会说OAuth Playground非常有用。有两个主要组件需要做到这一点:首先,你需要你的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="[email protected]"/> 
<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); 

我希望这可以帮助任何正在寻找这个答案的人。在操场上玩耍会帮助您找到正确的URL以及标题中所需的正确参数。

相关问题