我想使用PHP访问用户联系人。
我有两个名为index.php&的文件。在/ var / www / oauth目录中的validate.php。 重定向到validate.php后,它现在显示任何内容。 它是在“curl init之前”打印echo而不是在“curl init之后”打印echo。 请有人帮帮我。
Google帐户设置: -
Client ID: *********
Email address: *****
Client secret: ******
Redirect URIs: http://localhost/oauth/validate.php
JavaScript origins: http://localhost
这是我的PHP代码。
/var/www/oauth/index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login with gmail</title>
<style type="text/css">
body{background:url(random_grey_variations.png) repeat; }
#wrapper{width:960px; margin:0 auto; margin-top:100px;}
.button{display:block; height:40px; padding:0 20px; width:200px; background:#CCC; text-decoration:none; font:bold 18px Verdana, Geneva, sans-serif; color:#333; line-height:40px; text-align:center; margin-bottom:50px;}
</style>
</head>
<body>
<div id="wrapper">
<!-- <a href="http://webdevelopergeeks.com/tutorial/import-gmail-or-google-contacts-using-php-and-oauth-2-0/" class="button">Back To Article</a> -->
<a href="https://accounts.google.com/o/oauth2/auth?client_id=1015115465486.apps.googleusercontent.com&redirect_uri=http://localhost/oauth/validate.php&scope=https://www.google.com/m8/feeds/&response_type=code" class="button">Login Using Google</a>
</div>
</body>
</html>
/var/www/oauth/validate.php
<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='1015115465486.apps.googleusercontent.com'; // client id
$clientsecret='----'; //Secret id
$redirecturi='http://localhost/oauth/validate.php'; // 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
**echo "After curl init";**
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
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');
$result = $xml->xpath('//gd:email');
foreach ($result as $title) {
echo $title->attributes()->address . "<br><br>";
}
?>
</body></html>
答案 0 :(得分:5)
请检查php_curl
扩展程序是否已启用,因此cURL功能将适用于您。
如果已经启用,则发布详细的错误消息,以便我可以在最后检查。