• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
seegatesite header

Seegatesite.com

Seegatesite.com - Programming Tutorial , Sharing , How and Learn Together

  • TOOLS
    • Bootstrap Navbar Online Generator
    • Customize Sidebar Menu Bootstrap 3
    • Bootstrap Demo
  • ADVERTISE
  • CATEGORIES
    • Android
    • Blogging Tips
    • Database
    • CSS
    • Info Gadget
    • Javascript
    • Linux
    • PHP
    • Various
    • WordPress
  • Q&A
  • PHP
  • JAVASCRIPT
  • JQUERY
  • ANGULAR
  • WORDPRESS
  • SEO
  • REACT
🏠 » PHP » PHP Tutorial How To Use Google API To Access Google Plus Service

PHP Tutorial How To Use Google API To Access Google Plus Service

By Sigit Prasetya Nugroho ∙ August 7, 2017 ∙ PHP ∙ 1 Comment

Share : TwitterFacebookTelegramWhatsapp

Tutorial on how to use Google API PHP. Google provides a variety of useful APIs for web developers and desktop applications to take advantage of the features of their product. Google itself has provided a PHP library that can be used to access its API. For its use, please follow the following tutorial.

API can simply interpret as the program code which is the interface or liaison between the application or the web that we created with the service provided by the server. Similar to Google API, we can use the service from Google to access its products, such as accessing profile on Google plus, adding a post on Blogger using Post API, access Youtube movie and much more. Google API can be studied directly through the Google Developer site.

Read another article: Create Your Own Twitter Bot In PHP To Auto Tweet

Not long ago I was learning Google’s API to retrieve profile data from Google Plus. So, when we login to the web application, our profile data on Google Plus can be taken. So, this way can be an alternative if the user wants to register and does not need to fill in the input form to store user data. With just one button for access to its Google API, we get the user’s data. Let’s just start our tutorial

Table of Contents

  • 1 How To Use Google API With PHP
    • 1.1 # Create Google+ API Project And OAuth Client Id Credential
    • 1.2 # Create Simple App to use google API with Google PHP API CLient Library

How To Use Google API With PHP

# Create Google+ API Project And OAuth Client Id Credential

  1. Create a project on google API. Use the https://console.developers.google.com UrlHow To Create Google Api Project
  2. After creating project, Please go to the library page. Enable Google+ API service
    Go To Library Menu In The Google Api Project
    Go To Library Menu In The Google Api Project
    Choose Google+ Api Library
    Choose Google+ Api Library

    Enable Google+ Api Library
    Enable Google+ Api Library
  3. Create an OAuth Client ID Credential

    Create Oauth Client Id Credential For Google Api
    Create Oauth Client Id Credential For Google Api
  4. Read more video tutorial how to build google + API OAuth client id credential below

# Create Simple App to use google API with Google PHP API CLient Library

  1. To download Google API PHP Client, please use composer. For more information, please visit Github repo.
  2. Create a folder with the name “gplus,” then type the following composer.

    1
    composer require google/apiclient:^2.0

  3. Create an “index.php” file and copy the following script

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    <?php
    require_once 'vendor/autoload.php';
    session_start();
     
    $client = new Google_Client();
    $client->setApplicationName("Client_Library_Examples");
    $client->setClientId('Your-client-ID');
    $client->setClientSecret('Your-client-secret');
    $client->setRedirectUri('http://localhost/gplus/index.php');
    $client->setScopes(array('https://www.googleapis.com/auth/plus.login'));
     
    $plus = new Google_Service_Plus($client);
     
    # for logout
    if (isset($_REQUEST['logout'])) {
      unset($_SESSION['access_token']); # hapus access_token
    }
     
    # get content after connect to google account
    if (isset($_GET['code'])) {
      $token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
      $client->setAccessToken($token);
      // store in the session also
      $_SESSION['access_token'] = $token;
    }
     
    if ($client->getAccessToken()) {
      $profile = $plus->people->get('me');
    } else {
      $authUrl = $client->createAuthUrl();
    }
    ?>
    <!doctype html>
    <html>
    <head>
    </head>
    <body>
      <header><h1>Tutorial how to implement Google API using PHP by seegatesite.com </h1></header>
      <?php
      if( !empty($profile) ) :
        ?>
      <table>
        <tr>
          <td>Display Name</td>
          <td>:</td>
          <td><?php echo $profile['displayName']; ?></td>
        </tr>
        <tr>
          <td>Family Name</td>
          <td>:</td>
          <td><?php echo $profile['name']['familyName']; ?></td>
        </tr>
        <tr>
          <td>Given Name</td>
          <td>:</td>
          <td><?php echo $profile['name']['givenName']; ?></td>
        </tr>
        <tr>
          <td>BirthDay</td>
          <td>:</td>
          <td><?php echo date('d/m/Y',strtotime($profile['birthday'])); ?></td>
        </tr>
        <tr>
          <td>Occupation</td>
          <td>:</td>
          <td><?php echo $profile['occupation']; ?></td>
        </tr>
        <tr>
          <td>Tagline</td>
          <td>:</td>
          <td> <?php echo $profile['tagline']; ?></td>
        </tr>
        <tr>
          <td>Image Profile</td>
          <td>:</td>
          <td><img src="<?php echo $profile['image']['url']; ?>"/></td>
        </tr>
        <tr>
          <td>Sites</td>
          <td>:</td>
          <td>
            <ul>
              <?php for ( $i=0; $i<count($profile['urls']); $i++ ) : ?>
              <li><a href="<?php echo $profile['urls'][$i]['value']; ?>"><?php echo $profile['urls'][$i]['value']; ?></a></li>
            <?php endfor; ?>
          </ul>
        </td>
      </tr>
    </table>
    <?php
    endif;
    ?>
     
    <?php
    if(isset($authUrl)) {
      print "<a class='login' href='$authUrl'>Connect to your account!</a>";
    } else {
      print "<a class='logout' href='?logout'>Logout</a>";
    }
    ?>
    </body>
    </html>

  4. Save and run your application via the following link

    “http://localhost/gplus/”

If your application is correct, then the system will run like the following video

Explanation:

1. For Google PHP client library to work properly, please use the composer to get it
2. Initial configuration of the use of Google API

1
2
3
4
5
6
7
8
9
require_once 'vendor/autoload.php';
session_start();
 
$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
$client->setClientId('Your-client-ID');
$client->setClientSecret('Your-client-secret');
$client->setRedirectUri('http://localhost/gplus/index.php');
$client->setScopes(array('https://www.googleapis.com/auth/plus.login'));

3. Use the OAuth 2.0 Scopes for Google APIs link to get the google library authentication URL

4. Using the library to access google service is quite easy. For example to use google plus service, just use the following way

1
$plus = new Google_Service_Plus($client);

All Google Service Library

Then all the functions needed to access google plus service can be found in the “Resource” folder. For example to get people profile

How To Use Google Api With Php Library To Access Service

All reference API google plus you can access at https://developers.google.com/+/web/api/rest/latest/

The second example

If you want to access the Google API for Blogger using PHP

1
$plus = new Google_Service_Blogger($client);

Then all the functions needed to access the blogger API you can see in the “Resource” folder.

Php Tutorial Google Service Api For Blogger Using Php

To download the entire project please share this article from the button share below

LINK : https://seegatesite.com/gplus/

Password : seegatesite.com

Read another article: Tutorial Facebook SDK V4 for PHP for Beginners

Thus my article about PHP Tutorial How To Use Google API To Access Google Plus Service hope useful

 

Another PHP Related Post :

  • How To Replace String With Another String In PHP
  • Login Page – Tutorial CRUD Client and API Server Using JQuery And Lumen Part 2
  • Tutorial CRUD Client and API Server Using JQuery And Lumen Part 1
  • How To Solve Problems Illegal mix of collations (latin1_swedish_ci,IMPLICIT) In Laravel
  • How To Resolve No ‘Access-Control-Allow-Origin’ Header In Lumen
  • How To Create Custom Class In Laravel 5.5 For Beginners

Avatar for Sigit Prasetya Nugroho

About Sigit Prasetya Nugroho

This site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.

Reader Interactions

Comments

  1. Avatar for PunithPunith says

    January 20, 2020 at 11:22 am

    Please update this thing with the latest version

    Reply
    • Avatar for Sigit Prasetya NugrohoSigit Prasetya Nugroho says

      January 28, 2020 at 2:27 am

      google plus not working again..im sory

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Welcome to my Home,

Avatar for Sigit Prasetya NugrohoThis site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.



Popular Articles

Checked checkbox AdminLTE Bootstrap in Jquery

November 4, 2014 By Sigit Prasetya Nugroho 7 Comments

Simple create date format validation with jqueryUI

December 21, 2014 By Sigit Prasetya Nugroho Leave a Comment

Create Simple Progress Bar for Fake Online Generator with Jquery

January 10, 2015 By Sigit Prasetya Nugroho Leave a Comment

22+ Coolest Free Jquery Plugin For Premium Theme

October 3, 2015 By Sigit Prasetya Nugroho Leave a Comment

Easy Build Your Anti Copy Paste Plugin

October 6, 2015 By Sigit Prasetya Nugroho Leave a Comment

Popular Tags

adminlte (15) adsense (13) adsense tips (4) affiliate amazon (13) amazon (12) Android (8) angular (16) angular 4 (12) angular 5 (4) asin grabber (3) Bootstrap (27) codeigniter (5) create wordpress theme (5) crud (8) css (6) free wordpress theme (7) google adsense (4) imacros (4) increase traffic (6) jquery (34) laravel (10) laravel 5 (5) learn android (5) modal dialog (5) mysql (6) nodeJs (4) optimize seo (4) pdo (6) php (30) plugin (53) pos (7) Publisher Tips (5) react (3) Reactjs (7) SEO (37) theme (17) tutorial angular (5) tutorial angular 4 (6) tutorial javascript (10) tutorial javascript beginners (4) twitter (3) widget (3) wordpress (18) wordpress plugin (13) XMLRPC (5)




  • About
  • Contact Us
  • Disclaimer
  • Privacy Policy
  • Terms and Conditions

©2021 Seegatesite.com