php
  • api
  • oauth
  • 2012-08-12 125 views 3 likes 
    3

    我正在使用Oauth 2.0導入聯繫人,但我只收到電子郵件地址。任何方式來獲得其他領域?另外,如何使用Google API創建聯繫人。 只需使用PHP。如何使用PHP從Google獲取完整的聯繫信息?

    這裏是我的代碼:

    //setting parameters 
    $authcode= $_GET["code"]; 
    $clientid='xxxxxxx'; 
    $clientsecret='secret'; 
    $redirecturi='validate.php'; 
    $fields=array(
    'code'=> urlencode($authcode), 
    'client_id'=> urlencode($clientid), 
    'client_secret'=> urlencode($clientsecret), 
    'redirect_uri'=> urlencode($redirecturi), 
    'grant_type'=> urlencode('authorization_code') 
    ); 
    //url-ify the data for the POST 
    $fields_string=''; 
    foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; } 
    $fields_string=rtrim($fields_string,'&'); 
    //open connection 
    $ch = curl_init(); 
    //set the url, number of POST vars, POST data 
    curl_setopt($ch,CURLOPT_URL,'https://accounts.google.com/o/oauth2/token'); 
    curl_setopt($ch,CURLOPT_POST,5); 
    curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string); 
    // Set so curl_exec returns the result instead of outputting it. 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    //to trust any ssl certificates 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    //execute post 
    $result = curl_exec($ch); 
    //close connection 
    curl_close($ch); 
    //extracting access_token from response string 
    $response= json_decode($result); 
    $accesstoken= $response->access_token; 
    //passing accesstoken to obtain contact details 
    $xmlresponse= file_get_contents('https://www.google.com/m8/feeds/contacts/default  /full?oauth_token='.$accesstoken.'&max-results=5'); 
    //reading xml using SimpleXML 
    $xml= new SimpleXMLElement($xmlresponse); 
    $xml->registerXPathNamespace('gd', 'http://schemas.google.com/g/2005'); 
    $result = $xml->xpath('//gd:email'); 
    foreach ($result as $title) { 
        $addrss=$title->attributes()->address; 
        echo $addrss."<br><br>"; 
    

    回答

    3

    好吧,如果你只是XML語法gd:email,那麼你肯定只能獲得的電子郵件地址。請參閱Contact kind documentation以瞭解您還可以解析的元素的概述。

    有關創建聯繫人,則可以只發出與人體相同的端點的聯絡方式POST請求:

    https://www.google.com/m8/feeds/contacts/default/full 
    

    有關詳細的文檔上的聯繫方式與格式,請參見API documentation

    0

    PHP GMAIL Contacts XML Parsing with DOMDocument and cURL

    DNT知道添加聯繫人,但上面的鏈接應該讓你在正確的方向開始,創建一個DOMDocument類和追加節點連接到它(PHP手冊)。我認爲這是要走的路,保持與谷歌聯繫API的

    +0

    使用谷歌聯繫api的[link](https://developers.google.com/google-apps/contacts/v3/index)v3.0 ,而不是v2的gdata。還可以使用[link](https://developers.google.com/oauthplayground/)訪問聯繫人API(或其他api),將xml保存到本地磁盤並使用XML,因爲返回的數據可能會不堪重負 – ritin 2013-08-11 07:39:05

    相關問題