2016-02-03 61 views
0

我试图使用Contacts API来检索点击Response-callback.php底部的<a>的人的所有联系人。我正在使用Google App Engine,并且在GAE中启用了联系人API。我通过遵循一个教程得到了这段代码,所以很难自己发现错误。我没有使用PHP或GAE或任何API。这也是为什么我无法调试并检查某些值是否为空值的原因。如何使用Contacts API检索所有Google联系人?

当我启动网站。我什么都没有显示。甚至没有我使用的回声来测试我是否可以访问response-callback.php,H2也没有显示我只是获得了一个完全白页。

的app.yaml:

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

handlers: 

- url:/
    script: main.php; 

main.php:

<!DOCTYPE html> 
<html> 

<head> 
</head> 

<body> 
<h2>Hi! Stackoverflow!</h2> 
<?php include'response-callback.php'; 
echo "response-callback file: " $test; 
?> 

</body> 

</html> 

响应callback.php:

<?php 
session_start(); 

require_once 'google-api-php-client/src/autoload.php'; 

$test = 'WORKS!'; 

$google_client_id = 'SECRET'; 
$google_client_secret = 'SECRET'; 
$google_redirect_uri = 'SECRET'; 

//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.googleapis.com/auth/contacts.readonly'); 

$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://www.googleapis.com/oauth2/v4/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'], 
); 
} 
} 

$google_contacts = $return; 

unset($_SESSION['google_code']); 

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

全白页?那将是_White死亡屏幕_。请参阅[此问题](http://stackoverflow.com/questions/2914127/white-screen-of-death)的答案以获取可能的解决方案。 –

回答

0

您可以使用的var_dump(),看是否数组google_contacts实际上是填充的。将这个在main.php:

<?php 
var_dump($google_contacts); 
?> 

如果你在的var_dump的输出,你可以使用HTML表格和一个简单的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> 
相关问题