Search Google Plus Profiles using PHP and Google Plus API

1 comment
The main aim of this application is to go familiar with the Google Plus API usage. Google Plus provides an extremely easy to use API to access the public contents on it. Here we will create a simple search application to search user profiles on Google Plus. See the live demo.


Live Demo Download Script
Follow the below given steps to create the application:
1. Go to code.google.com/apis/console/ and click on 'Create Project'. Enable on the 'Google+ API' from the list.
2. Go to 'API Access' tab and click on 'Create an OAuth 2.0 client id'. Provide the 'Project name' and other details.

3. In the 'Create Client ID' from fill in the 'Redirect Uri' and 'JavaScript Origin'. The 'Redirect Uri' is the page on which you want to redirect the user after successful login. 'JavaScript Origin' is the server address.

4. In the next step you will get the necessary information like Client ID, Client Secret etc.
5. Now download the Google Plus client library at http://code.google.com/p/google-api-php-client/.
6. Create the configuration file as follows:
<?php
require_once 'library/apiClient.php';
require_once 'library/contrib/apiPlusService.php';

session_start();

$client = new apiClient();
$client->setApplicationName("WebSpeaks Google Plus Search App");
//Set you API Credentials
$client->setClientId('857300729325-lbigd4k55u7964btojjaeoqi9na21m7g.apps.googleusercontent.com');
$client->setClientSecret('ZiOc-2tl8doRyJe8K7zFk3HA');
$client->setRedirectUri('http://localhost/blogger/gplus/me.php');
$plus = new apiPlusService($client);

if (isset($_REQUEST['logout'])) {
 unset($_SESSION['access_token']);
}

if (isset($_GET['code'])) {
 $client->authenticate();
 $_SESSION['access_token'] = $client->getAccessToken();
 header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}

if (isset($_SESSION['access_token'])) {
 $client->setAccessToken($_SESSION['access_token']);
}

//Here we fetch the serach query from the form
if (isset($_REQUEST['search']) && !empty($_REQUEST['search'])) {
 $search = urldecode($_REQUEST['search']);
} else {
 $search = 'Arvind Bhardwaj';
}
?>
This application will request an authorization from you to access your Google profile as:

In the next step you will get an array of users search results which will look something like:


7. Create the search results page as:
<?php
include "config.php";

if ($client->getAccessToken()) {
 //Set the number of results per page
 $optParams = array('maxResults' => 10);

 //If next page token is set, add this to serach options
 //It will get the results from the next page
 if (isset($_REQUEST['pageToken']) && !empty($_REQUEST['pageToken'])) {
  $optParams['pageToken'] = $_REQUEST['pageToken'];
 }

 //Get the results
 $results = $plus->people->search($search, $optParams);
 
 // The access token may have been updated lazily.
 $_SESSION['access_token'] = $client->getAccessToken();
} else {
 $authUrl = $client->createAuthUrl();
}
?>
<!doctype html>
<html>
<head>
 <meta charset="utf-8">
 <link rel='stylesheet' href='style.css' />
 <title>WebSpeaks Google Plus Search App</title>
</head>
<body>
<header><h1>WebSpeaks Google Plus Search App</h1></header>
<div class="box">
<form method="get" action="">
 <input type="text" name="search" value="<?php echo $search ?>" /><input type="submit" value="Search" class="button" />
</form>
<?php
if (isset($results['items']) && !empty($results['items'])) {
 foreach ($results['items'] as $result) {
  $url = filter_var($result['url'], FILTER_VALIDATE_URL);
  $img = filter_var($result['image']['url'], FILTER_VALIDATE_URL);
  $name = filter_var($result['displayName'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
  ?>
  <div class="row">
   <div class="col1"><img src="<?php echo $img ?>"></div>
   <div class="col2"><a href="<?php echo $url ?>" target="_blank"><?php echo $name ?></a></div>
  </div>
  <?php
 }
}
?>
<?php
if (isset($results['nextPageToken']) && !empty($results['nextPageToken'])) {
 $next = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] .'?search='.urlencode($search).'&pageToken='. $results['nextPageToken'];
 echo '<div class="paginate"><a href="'.$next.'">Next</a></div>';
}
?>

<?php
 if(isset($authUrl)) {
  print "<a class='login' href='$authUrl'>Connect Me!</a>";
 } else {
  print "<div class='logout'><a href='?logout'>Logout</a></div>";
 }
?>
</div>
</body>
</html>

1 comments:

Great post, thank you! The static pages info was what I needed most. You coding is good. ics and php

We would love to hear from you...

back to top