I want to access user contacts using PHP.
I am having two files named index.php & validate.php in /var/www/oauth directory. After redirecting to validate.php,it is now showing any content. it is printing echo "before curl init" but not printing echo "after curl init". Please someone can help me out.
Google account setting:-
Client ID: *********
Email address: *****
Client secret: ******
Redirect URIs: http://localhost/oauth/validate.php
JavaScript origins: http://localhost
Here is my PHP code.
/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>