使用 php 导入 Gmail 联系人

importing gmail contacts using php

可能与之前的 post 造成了很多混淆,所以我正在尝试根据我的要求和努力重新设计我的整个 post。

我想使用 PHP 获取 GMAIL 联系人列表,为此我做了以下事情:

  1. 使用 oAuth 2.0 协议创建 Google 客户端 ID
  2. 创建 2 PHP 个文件(oAuth.php 和 index.php)

这是 2 个文件的代码:

oAuth.php:

<html>
    <head>
        <meta name="robots" content="noindex" />
        <title>Email address list - Import Gmail or Google contacts</title>
    <style type="text/css">
        a:link {color:Chocolate;text-decoration: none;}
        a:hover {color:CornflowerBlue;}
        .logo{width:100%;height:110px;border:2px solid black;background-color:#666666;}
    </style>
    </head>
    <body>
        <div class="logo" >
        </div>
<br/>
        <div><b>Visit Tutorial: </b><a style="font-size:17px;" href="" >Import Gmail or Google contacts using Google Contacts Data API 3.0 and OAuth 2.0 in PHP</a></div>
            <br/>
        <div style="padding-left: 50px;">
<?php
$client_id = '1041526369396-kmnbhsos616eqcqimjrs07icn2c176ln.apps.googleusercontent.com';
$client_secret = '*************';
$redirect_uri = 'https://www.test.multicon.in/test9/oauth.php';
$max_results = 25;

$auth_code = $_GET["code"];

function curl_file_get_contents($url)
{
 $curl = curl_init();
 $userAgent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)';

 curl_setopt($curl,CURLOPT_URL,$url);   //The URL to fetch. This can also be set when initializing a session with curl_init().
 curl_setopt($curl,CURLOPT_RETURNTRANSFER,TRUE);    //TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.
 curl_setopt($curl,CURLOPT_CONNECTTIMEOUT,5);   //The number of seconds to wait while trying to connect.    

 curl_setopt($curl, CURLOPT_USERAGENT, $userAgent); //The contents of the "User-Agent: " header to be used in a HTTP request.
 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);  //To follow any "Location: " header that the server sends as part of the HTTP header.
 curl_setopt($curl, CURLOPT_AUTOREFERER, TRUE); //To automatically set the Referer: field in requests where it follows a Location: redirect.
 curl_setopt($curl, CURLOPT_TIMEOUT, 10);   //The maximum number of seconds to allow cURL functions to execute.
 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); //To stop cURL from verifying the peer's certificate.
 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);

 $contents = curl_exec($curl);
 curl_close($curl);
 return $contents;
}

$fields=array(
    'code'=>  urlencode($auth_code),
    'client_id'=>  urlencode($client_id),
    'client_secret'=>  urlencode($client_secret),
    'redirect_uri'=>  urlencode($redirect_uri),
    'grant_type'=>  urlencode('authorization_code')
);
$post = '';
foreach($fields as $key=>$value) { $post .= $key.'='.$value.'&'; }
$post = rtrim($post,'&');

$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,'https://accounts.google.com/o/oauth2/token');
curl_setopt($curl,CURLOPT_POST,5);
curl_setopt($curl,CURLOPT_POSTFIELDS,$post);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,0);
$result = curl_exec($curl);
curl_close($curl);

$response =  json_decode($result);
$accesstoken = $response->access_token;

$url = 'https://www.google.com/m8/feeds/contacts/default/full?max-results='.$max_results.'&oauth_token='.$accesstoken;
$xmlresponse =  curl_file_get_contents($url);
if((strlen(stristr($xmlresponse,'Authorization required'))>0) && (strlen(stristr($xmlresponse,'Error '))>0))
{
    echo "<h2>OOPS !! Something went wrong. Please try reloading the page.</h2>";
    exit();
}
echo "<h3>Email Addresses:</h3>";
$xml =  new SimpleXMLElement($xmlresponse);
$xml->registerXPathNamespace('gd', 'http://schemas.google.com/g/2005');
$result = $xml->xpath('//gd:email');

foreach ($result as $title) {
  echo $title->attributes()->address . "<br>";
}
?>
</div>
</body></html>

和index.php

<html>
    <head>
        <meta name="robots" content="noindex" />
        <title>Import Gmail or Google contacts using Google Contacts Data API 3.0 and OAuth 2.0</title>
    <style type="text/css">
        a:link {color:Chocolate;text-decoration: none;}
        a:hover {color:CornflowerBlue;}
        .logo{width:100%;height:110px;border:2px solid black;background-color:#666666;}
    </style>
    </head>
    <body>
        <div class="logo" >

        </div>
<br/>
        <div><b>Visit Tutorial: </b><a style="font-size:17px;" href="http://test.multicon.in/test9/" >Import Gmail or Google contacts using Google Contacts Data API 3.0 and OAuth 2.0 in PHP</a></div>
            <br/><br/>
        <div align="center" >
            <a  style="font-size:25px;font-weight:bold;" href="https://accounts.google.com/o/oauth2/auth?client_id=1041526369396-kmnbhsos616eqcqimjrs07icn2c176ln.apps.googleusercontent.com&redirect_uri=https://www.test.multicon.in/test9/oauth.php&scope=https://www.google.com/m8/feeds/&response_type=code">Click here to Import Gmail Contacts</a>
        </div>
    </body>
</html>

现在,当我点击 index.php 中的 link 时,它显示我的项目想要访问我的 Gmail 联系人...但是当它把我带到重定向的页面时...它显示页面不可用,而该页面在那里。我不知道你是否清楚...如果可能请检查 link http://test.multicon.in/test9/index.php 可能这将帮助你理解我的问题

终于...我已经解决了这个问题,我的域没有添加 WWW 记录,这就是我收到错误 "DNS_PROBE_FINISHED_NXDOMAIN" 的原因,我为 [=13] 添加了 WWW 记录=],错误已解决