2013-01-24 205 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以及標題中所需的正確參數。

相關問題