我的 php 不会做任何事情?使用联系人 API 共 google

My php wont do anything? Using Contacts API of google

我正在尝试制作一个网站,您可以在其中从 gmail 导入联系人并将他们添加为网站内的朋友。现在我只想显示点击 <a> 的人的联系人。我遵循了一个教程,一切都应该有效,但事实并非如此。它确实在顶部显示了 <H2>,但没有显示实际上很重要的 <a>。我的 google 秘密和客户端 ID 是正确的,但我不想只显示它们。 google php API 的路径也是正确的,但我认为那里出了问题,因为它无法识别任何 php.

app.yaml:

application: csimporttest
version: 1
runtime: php55
api_version: 1
threadsafe: yes

handlers:

- url: .*
  script: response-callback.php

响应-callback.php:

<!DOCTYPE html>
<html><body>
<h2>ttest</h2>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

<script src="https://apis.google.com/js/client.js"></script>

<?php
session_start();

//include google api library
require_once 'google-api-php-client/src/Google/autoload.php';// or wherever autoload.php is located

$google_client_id = 'Google Client Id';
$google_client_secret = 'Google Secret';
$google_redirect_uri = 'https://csimporttest.appspot.com';

//setup new google client
$client = new Google_Client();
$client -> setApplicationName('csimporttest');
$client -> setClientid($google_client_id);
$client -> setClientSecret($google_client_secret);
$client -> setRedirectUri($google_redirect_uri);
$client -> setAccessType('online');

$client -> setScopes('https://www.google.com/m8/feeds');

$googleImportUrl = $client -> createAuthUrl();

function curl($url, $post = "") {
 $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.
 if ($post != "") {
 curl_setopt($curl, CURLOPT_POST, 5);
 curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
 }
 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, FALSE);
 //To stop cURL from verifying the peer's certificate.
 $contents = curl_exec($curl);
 curl_close($curl);
 return $contents;
}

if(isset($_SESSION['google_code'])) {
 $auth_code = $_SESSION['google_code'];
 $max_results = 200;
    $fields=array(
        'code'=>  urlencode($auth_code),
        'client_id'=>  urlencode($google_client_id),
        'client_secret'=>  urlencode($google_client_secret),
        'redirect_uri'=>  urlencode($google_redirect_uri),
        'grant_type'=>  urlencode('authorization_code')
    );
    $post = '';
    foreach($fields as $key=>$value)
    {
        $post .= $key.'='.$value.'&';
    }
    $post = rtrim($post,'&');
    $result = curl('https://accounts.google.com/o/oauth2/token',$post);
    $response =  json_decode($result);
    $accesstoken = $response->access_token;
    $url = 'https://www.google.com/m8/feeds/contacts/default/full?max-results='.$max_results.'&alt=json&v=3.0&oauth_token='.$accesstoken;
    $xmlresponse =  curl($url);
    $contacts = json_decode($xmlresponse,true);

 $return = array();
 if (!empty($contacts['feed']['entry'])) {
 foreach($contacts['feed']['entry'] as $contact) {
           //retrieve Name and email address  
 $return[] = array (
 'name'=> $contact['title']['$t'],
 'email' => $contact['gd$email'][0]['address'],
 );
 } 
 }

echo ';print_r($google_contacts);echo ';

 unset($_SESSION['google_code']);

}
?>

<a href="<?php echo $googleImportUrl; ?>"> Import google contacts </a>

<?php

 if (!empty($contacts['feed']['entry'])) {
 foreach($contacts['feed']['entry'] as $contact) {
            //retrieve Name and email address  
 $return[] = array (
 'name'=> $contact['title']['$t'],
 'email' => $contact['gd$email'][0]['address'],
 );

            //retrieve user photo
 if (isset($contact['link'][0]['href'])) {

 $url =   $contact['link'][0]['href'];

 $url = $url . '&access_token=' . urlencode($accesstoken);

 $curl = curl_init($url);

         curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
         curl_setopt($curl, CURLOPT_TIMEOUT, 15);
 curl_setopt($curl, CURLOPT_VERBOSE, true);

         $image = curl_exec($curl);
         curl_close($curl);

 }

                        $return['image'] = $image;

                        echo '';

 } 
 }

?>

</html></body>

首先,您是对的 - 从不 显示您的客户端 ID 和密码。

关于您发布的内容,有很多事情不清楚,包括您如何确切地获取 google_code 会话变量集,但我建议按照以下几行进行调试:

  • 查看 $result 的完整值。它可能会返回一个错误代码,这可能表明它没有加载联系人的原因。
  • 仔细检查令牌 URL 端点。 https://developers.google.com/identity/protocols/OAuth2WebServer 说它应该是 https://www.googleapis.com/oauth2/v4/token,但我以前看到 URL 列出的不同,所以我不知道哪个是实际有效的。
  • 确保您已在 Google Developers Console 中授权联系人 API,并确保在您通过授权 window 时,它会请求访问您的权限联系人。

您可以使用 var_dump() 来查看数组 google_contacts 是否已实际填充。把这个放在 main.php:

<?php
var_dump($google_contacts);
?>

如果您在 var_dump 中获得输出,您可以使用 html table 和简单的 foreach,请看下面:

<table border='1' class="table table-striped table-condensed">
<thead>
<tr>
    <th>Naam </th>
    <th>Email </th>
</tr>
</thead>
<tbody>
<?php
foreach ($google_contacts as $contacts){
    echo'<tr>';
    echo'<td>'. $contacts['name']."</td>";
    echo'<td>'. $contacts['email'].'</td>';
    echo'</tr>';
}
?>
</tbody>

</table>