Please wait for 15 seconds...

Wednesday, 14 November 2012



Google has changed the old method to get Google plus user’s profile picture which was below.

https://profiles.google.com/s2/photos/profile/GOOGLE_PLUS_USER_ID?sz=IMAGE_DIMENTION

And

https://www.googleapis.com/plus/v1/people/GOOGLE_PLUS_USER_ID?fields=image&key=YOUR_API_KEY

But unfortunately none of above are working.

Right now Google allow it only through oauth. The same way you get authentication for user’s personal and profile information. But I have made some twist here so you can get user’s profile image and store it on server without any hurdle.  In future you can use profile image whenever you want without knocking to google.


First download google library folder. Now create get_profile_image.php file and copy n paste below code in it.

get_profile_image.php


<?php
require_once 'g_api_php_client/src/Google_Client.php';
require_once 'g_api_php_client/src/contrib/Google_PlusService.php';

$user_info = array();
$msg = '';
$profile_img = '';
$success = false;
/*************** Google Oauth Credentials ***************/
$config['app_name'] = 'APP_NAME';# Your app name in console.google.com
$config['client_id'] = 'CLIENT_ID'; #for web application
$config['client_secret'] = 'CLIENT_SECRET';
$config['redirect_uri'] = 'REQUEST_URI';
$config['developer_key'] = 'API_KEY';
/********************************************************/
$client = new Google_Client();
$client->setApplicationName($config['app_name']);
$client->setClientId($config['client_id']);
$client->setClientSecret($config['client_secret']);
$client->setRedirectUri($config['redirect_uri']);
$client->setDeveloperKey($config['developer_key']);
$scope[] = 'https://www.googleapis.com/auth/userinfo.profile';
$client->setScopes($scope);

if(isset($_GET['login'])){
    header('location:'.$client->createAuthUrl());
}
elseif(isset($_GET['code'])){
$client->authenticate();
$token = $client->getAccessToken();

$token_json = json_decode($token);
$url = "https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token=".$token_json->access_token;
$user_info = json_decode(curl_function($url), true);

if(!empty($user_info['picture'])){
$success = true;

$profile_img = 'profile_image_'.mt_rand(11111111, 99999999).'.jpg';

copy($user_info['picture'], $profile_img);

$msg = "<p>Profile image has been stored. Click <a href='".$profile_img."'>here</a> to see full size image.</p>";
$msg .= "<p><img src='".$profile_img."'</p>";
}
else{
$msg = "<p>Unable to get profile image. Please try again.</p>";
}
}
function curl_function($url){
    $ch = curl_init();
    $timeout = 10;
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
    $response = curl_exec($ch);
    curl_close($ch);
    return $response;
}

include  'get_profile_image.html';
?>

Create get_profile_image.html file and copy n paste below code in it.

get_profile_image.html


<!DOCTYPE>
<html>
<head>
<title>Get Google Plus Profile Picture</title>
<link rel="shortcut icon" href="images/others/favicon.ico">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="I am explaining you here how to get google plus profile picture">
<meta name="author" content="https://plus.google.com/+AtulPrasadGupta24">
<style type="text/css">
.main{
width: 600px;
height:300px;
position:absolute;
left:50%;
margin-left:-300px;
top:50%;
margin-top:-150px;
}
.msg{
padding:7px;
}
.login_btn_hold{

}
.login_btn{
width: 525px;
text-decoration:none;
}
.focused{
padding: 7px 12px 7px 12px;
background-color: #C53228;
color: white;
font-weight: bold;
font-size:25pt;
}
.focused a{
color: white;
}
.focused img{
width:100px;
}
</style>
</head>
<body>
<div class="main">
<div class="msg <?php if($success){?>focused<?php }?>"><?=$msg?></div>
<div class="login_btn_hold <?php if(!$success){?>focused<?php }?>">
<a href="index.php?login=true" class="login_btn">Click to Get Google Plus Profile Image</a>
</div>
</div>
</body>
</html>

Now go to console.developers.google.com and setup & create a project(app). I have explained here how to setup for Google project(app).

Save files and run get_profile_image.php in your browser. Click on Get Google Plus Profile Image button and see the system running.

Posted by Atul Prasad Gupta

1 comment:

  1. There's a far easier way to do this - check out http://www.avatarapi.com/

    - It gets the user's pic and profile name from it's email address, the docs have a 3 line sample in PHP.

    ReplyDelete

Subscribe Techsirius

Techsirius on Facebook