Showing posts with label API. Show all posts
Showing posts with label API. Show all posts

Sunday 8 December 2019

Swagger - REST API documentation tool

Swagger - REST API documentation tool


Question: What is swagger?
It is REST API documentation tool which is used to list the APIs and view the request and response.
In this, we need to add some annotations in your functions and then it will automatically generate a beautiful and user-friendly documentation for your APIs.



Question: Where it is used?
Whenever we develop an android, IOS (Apple) application, we need to create an APIs for them.

To check the API details (URL, Request, response), we use Swagger.


Question: Who is using swagger?
Backend Developer: developer used it while creating Or updating an API.
Team Leader: As it give List of APIs with request and response. Team leader used it review the APIs.
Android/IOS Developer: While integrating the APIs, Its quite help the developer because they can check the response of APIs with different request.


Question: What are benefits of swagger?
  1. Its give list of APIs with short description.
  2. Its give the Request and Response of API.
  3. We can test the response of API with different request.
  4. We need to integrate in application first time, after the we need to add some annotations in functions/APIs.
  5. Can be integrate in Core PHP or Framework.
  6. We need not to list and details of apis manually.



Question: What is Swagger Editor?
The Swagger Editor is the tool for quickly getting started with the Swagger Specification.
Create new APIs OR update existing ones in a powerful Editor which visually renders your Swagger definition and provides real time error-feedback.
http://editor.swagger.io/#/


Question: Give a Demo URL?
http://petstore.swagger.io/


Question: From where i can download?
http://swagger.io/docs/#swagger-ui-documentation-29


Question: How to Add API in swagger?
Add Following code before the class declaration
/**
 * @SWG\Resource(
 *     apiVersion="1.0",
 *     swaggerVersion="1.2",
 *     description="Search users"
 * )
 */



Question: How to add new Operation for an API?
Add Following code before the method declaration
    /**
     * @SWG\Api(
     *     path="/search",
     *     @SWG\Operation(
     *         method="POST",
     *         summary="Get users near your location",
     *         type="Users list",
     *         @SWG\Parameters(
     *             @SWG\Parameter(
     *                 name="interests",
     *                 paramType="form",
     *                 description="comma seperated looking for",
     *                 required=false,
     *                 type="string",
     *                 minimum="1.0"
     *             ),
     *             @SWG\Parameter(
     *                 name="page",
     *                 paramType="form",
     *                 description="page number",
     *                 required=true,
     *                 type="string",
     *                 minimum="1.0"
     *             ),
     *         ),
     *         @SWG\ResponseMessage(code=400, message="Invalid Data")
     *     )
     * )
     */



Saturday 16 November 2019

How to Send Tweets automatically with PHP?

How to Send Tweets automatically with PHP?


Basic Requirements
  1. An Twitter account.
  2. An Twitter app. If don't have please create @ https://apps.twitter.com
  3. Twitter Auth REST API, If don't have please download from https://github.com/abraham/twitteroauth
  4. An Server, You can also test in local wampserver or xampserver


How to Send Tweets automatically with PHP, Follow following Steps:
  1. Include twitter files.
    require_once($_SERVER['DOCUMENT_ROOT'].'/twitteroauth/twitteroauth.php');
    require_once($_SERVER['DOCUMENT_ROOT'].'/twitteroauth/config.php');
  2. Collect all constants values from your twitter app (i.e apps.twitter.com)

  3. Add constants in TwitterOAuth constructor.
    define('CONSUMER_KEY', 'YOUR_KEY');
    define('CONSUMER_SECRET', 'YOUR_SECRET_KEY');
    define('ACCESS_TOKEN', 'YOUR_TOKEN');
    define('ACCESS_TOKEN_SECRET', 'YOUR_TOKEN_SECRET');
    
  4. $twitter = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET,ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
  5. Now post the message.
    $message='This is test tweet.';
    $twitter->post('statuses/update', array('status' => $message));
  6. Refresh the twitter.com, you will see your message at top of page.

Thursday 14 June 2018

Get the access token with OAuth 2.0 for Google API


Follow Below 4 steps to get the access token with OAuth 2.0 for Google API


1. Obtain OAuth 2.0 credentials ("Client ID" and "Secret Key") from the Google Developers Console.
  • Go to https://console.developers.google.com/
  • Select your project (create new project, If not have created before).
  • Click on "APIs & auth"
  • Click to "APIs", Now you have all the list of Google APIs  .
  • Select OR Search the APIs from search box, the one which you want to use.
  • Select API and Click on "Enable API". (I enable "Google Webmaster Tools API")
  • Now, click on "Credentials", Click on "Create new Client ID" under OAuth tab.
  • Popup will come. click on "Configure consent screen".
  • New form will appear. Fill the details and click on "Save".
  • Confirmation page appear, Click on "Create Client ID"
  • Now you will see the screen simpilar to below:

 Obtain OAuth 2.0 credentials
 Obtain OAuth 2.0 credentials
  • m. Get the "client Id" and "Client secret". (DON'T SHARE IT).


2. Create a new page in PHP like http://www.domain.com/auth-url.php
Add this URL in "Authorized redirect URIs" in your project (in Edit setting) above created.


3. Get the GET Access token from google using "Client ID" and "Secret Key"
Add the below code in "auth-url.php"
  define(YOUR_CLIENT_ID,'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
  define(YOUR_CLIENT_SECRET,'xxxxxxxxxxxxxxx');
if (isset($_GET['code'])) {    
    $code = $_GET['code'];
    //Set the Auth URL
    $url = 'https://accounts.google.com/o/oauth2/token';
    
    //Set the Auth Parameter
    $redirectUri='http://' . $_SERVER["HTTP_HOST"] . $_SERVER["PHP_SELF"];
    $params = array(
        "client_id" => YOUR_CLIENT_ID,
        "client_secret" => YOUR_CLIENT_SECRET,
        "redirect_uri" => $redirectUri,
        "grant_type" => "authorization_code",
        "code" => $code,
    );

    /** Init the curl */
    $ch = curl_init();
    curl_setopt($ch, constant("CURLOPT_" . 'URL'), $url);
    curl_setopt($ch, constant("CURLOPT_" . 'POST'), true);
    curl_setopt($ch, constant("CURLOPT_" . 'POSTFIELDS'), $params);
    $output = curl_exec($ch);
    $info = curl_getinfo($ch);
    curl_close($ch);
    if ($info['http_code'] === 200) {
        header('Content-Type: ' . $info['content_type']);
        return $output;
    } else {
        die('An error occured');
    }
    /** Init the curl */
    
} else {

    $url = "https://accounts.google.com/o/oauth2/auth";

    //Set the Auth Parameter
    $params = array(
        "response_type" => "code",
        "client_id" => YOUR_CLIENT_ID,
        "redirect_uri" => 'http://' . $_SERVER["HTTP_HOST"] . $_SERVER["PHP_SELF"],        
        "scope" => "https://www.googleapis.com/auth/webmasters.readonly" //I have added scope for webmaster tool
        
    );

    $requestTo = $url . '?' . http_build_query($params);

    //Redirect the page
    header("Location: " . $requestTo);
} 



4. You will get result simpliar to below.
{ "access_token" : "ya29.kgGOtdWEj32NOSxLWkZXAaXAagmkP-4WgHEd8gpUfuelHD_lslquKzHMVV2OnnNc5h1BKWkY8aeCrA", "token_type" : "Bearer", "expires_in" : 3600 }

Here you got access_token, token_type and expires_in (in Seconds).


If this code does not work and need any assistance, Please feel free to comment.

Tuesday 5 September 2017

Validate Icalendar File or URL




Question: What is Desktop iCalendar?
Desktop iCalendar is a handy desktop calendar for Windows. It stays on your desktop and shows the days of the current month have the events. It can be sync with Google Calendar OR Yahoo Calendar. You can share the calendars with your family and friends with use of the Google OR Yahoo. File format of iCalendar is .ics.


Question: What is use of iCalendar?
iCalendar is a computer file format which allows Internet users to send meeting requests and tasks to other Internet users, via facebook, email. Recipients of the iCalendar data file can respond to the sender easily or counter propose another meeting date/time


Question: Why need of Validate the Icalendar?
When we validate, It make sure us that we are sharing correct and valid file. Also it make sure recipients will get the  task/events once they get.


Question: How to validate Icalendar File OR code snippt.
http://severinghaus.org/projects/icv/


Question: What are Development Tools for Validate Icalender.
Language Library
C/C++ libical
Java iCal4J
Python vObject
Ruby iCalendar
Ruby RiCal

Monday 9 January 2017

How to integrate Google Places Search API in website?

How to integrate Google Places Search API in website?

Get the Google API Key
  1. Login to https://console.developers.google.com
  2. Get the API Keys.
  3. Enable "Google Places API Web Service" from Google Google Map API



HTML Code

<script src="https://maps.googleapis.com/maps/api/js?v=3&amp;libraries=places&amp;key=HERE_YOUR_KEYS"></script>
<div class="search-Form">
<input id="search" name="q" type="text" value="" /> <input id="submit" type="submit" value="go" />
</div>



JavaScript Code

    var lat
    var lon
    function initialize() {
        var input = document.getElementById('search');
        var autocomplete = new google.maps.places.Autocomplete(input);

        google.maps.event.addListener(autocomplete, 'place_changed', function() {
            var place = autocomplete.getPlace();
            lat = place.geometry.location.lat()
            lon = place.geometry.location.lng()
        });

    }

    google.maps.event.addDomListener(window, 'load', initialize);

View Demo