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
How To Use Google API With PHP
# Create Google+ API Project And OAuth Client Id Credential
- Create a project on google API. Use the https://console.developers.google.com Url
- After creating project, Please go to the library page. Enable Google+ API service
Go To Library Menu In The Google Api Project Choose Google+ Api Library Enable Google+ Api Library - Create an OAuth Client ID Credential
Create Oauth Client Id Credential For Google Api - 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
- To download Google API PHP Client, please use composer. For more information, please visit Github repo.
- Create a folder with the name “gplus,” then type the following composer.1composer require google/apiclient:^2.0
- Create an “index.php” file and copy the following script123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102<?phprequire_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 logoutif (isset($_REQUEST['logout'])) {unset($_SESSION['access_token']); # hapus access_token}# get content after connect to google accountif (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><?phpif( !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><?phpendif;?><?phpif(isset($authUrl)) {print "<a class='login' href='$authUrl'>Connect to your account!</a>";} else {print "<a class='logout' href='?logout'>Logout</a>";}?></body></html>
- 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); |
Then all the functions needed to access google plus service can be found in the “Resource” folder. For example to get people profile
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.
To download the entire project please share this article from the button share below
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
Please update this thing with the latest version
google plus not working again..im sory