如何获得姓名,电子邮件和使用Oauth& amp; PHP

时间:2014-02-04 10:31:45

标签: php email curl oauth google-oauth

我正在尝试获取姓名,电子邮件,地址和电话没有Gmail联系人。 我能够获得联系人的电子邮件ID,但不能获取其他内容。 任何人都可以告诉我如何获得名称,地址和电话号码没有联系。 我尝试使用gd:name& atom:title但没什么用。 这是我的代码。

<html>
    <head><meta name="robots" content="noindex" /></head>
    <body style="font-family: tahoma; font-size: 12px;">
        <a href="http://webdevelopergeeks.com/tutorial/import-gmail-or-google-contacts-using-php-and-oauth-2-0/">Visit Article</a><hr>
<?php
//setting parameters
$authcode= $_GET["code"];
$clientid='******';
$clientsecret='******'; // replace by your client secret
$redirecturi='http://localhost/oauth/validate.php'; // replace by your redirect uri [path to your validate.php]
// echo $authcode;
$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
echo "before curl init";
$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);
// echo "After curl init";
//close connection
curl_close($ch);
// echo $result;

//extracting access_token from response string
$response=  json_decode($result);
$accesstoken= $response->access_token;
//passing accesstoken to obtain contact details
// echo $accesstoken;
$xmlresponse= file_get_contents('https://www.google.com/m8/feeds/contacts/default/full?oauth_token='.$accesstoken);
//reading xml using SimpleXML
$xml= new SimpleXMLElement($xmlresponse);
$xml->registerXPathNamespace('gd', 'http://schemas.google.com/g/2005');
// echo "<pre>";                //print array to console
//     {
//      print_r($xml);
//     }
//   echo "</pre>";
// echo "working";
$resultemail = $xml->xpath('//gd:email');
$resultname = $xml->xpath('//gd:name');
// echo $resultname;
echo "<pre>";                //print array to console
    {
        print_r($resultname);
    }
  echo "</pre>";
// echo "working";
// foreach ($resultemail as $title) {
//   echo $title->attributes()->address . "<br><br>";
// }


?>
</body></html>

感谢。

0 个答案:

没有答案