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 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
6. Create the configuration file as follows:
require_once 'library/apiClient.php';
require_once 'library/contrib/apiPlusService.php';


$client = new apiClient();
$client->setApplicationName("WebSpeaks Google Plus Search App");
//Set you API Credentials
$plus = new apiPlusService($client);

if (isset($_REQUEST['logout'])) {

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

if (isset($_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:
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>
 <meta charset="utf-8">
 <link rel='stylesheet' href='style.css' />
 <title>WebSpeaks Google Plus Search App</title>
<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" />
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>
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>';

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


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