2017 © Pedro Peláez
 

library petitions-api

This package provides a wrapper for Petitions.io REST Api

image

cloudwelder/petitions-api

This package provides a wrapper for Petitions.io REST Api

  • Friday, July 28, 2017
  • by reneesh
  • Repository
  • 5 Watchers
  • 0 Stars
  • 26 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 13 Versions
  • 0 % Grown

The README.md

Petitions.io API PHP Wrapper

This is PHP wrapper to Petitions.io API, (*1)

Contents

Installation

Installation is done using composer. Run the following command to include this package into your project
composer require cloudwelder/petitions-api, (*2)

Pre-requisites

In order to use this package, you must have an active App at petitions.io. If you dont already have one, register for one at petitions.io After the registration, note down your client_id, client_secret and redirect_uri OR you could get a personal access token directly., (*3)

Usage

Make sure you have the following line in your code before using any of the API classes. Thi is not required if you are using a framework such as laravel., (*4)

require "vendor/autoload.php"

Initializing the API.

To use any of the APi calls, you must first get an instance of the API and acquier an access token using any of the following methods., (*5)

Methods 1: Using API credentials.

use CloudWelder/PetitionsApi/PetitionsApi;

$api = new PetitionsApi('client_id', 'client_secret', 'reidrect_uri');

Substitute client_id, client_secret, redirect_uri with your own values., (*6)

Generating Login URL

To gain access rights from a user, you must first redirect him to petitions.io. You can use the getRedirectUrl() method to generate this url., (*7)

$redirectUrl = $api->generateRedirectUrl();

You can then use this url to reidrect the user or as the href of an html link., (*8)

echo "<a href='$redirectUrl'>Click here to login</a>";

Acquiring Access Token

Once the user has granted your app's access request, petitions.io will redirect the user to the url you have specified in redirect_uri settings in petitions.io. This is where you should acquire the access token using the autthorization code trnsmitted as part of the url., (*9)

$token = $api->generateTokenFromCode($_GET['code']);
$accessToken = $token->getAccessToken();

Store this access_token into data base or other persistant storage to use it later. Unless the user revokes the grant, this access token is valid for a long time (a year), (*10)

The Token object retuned will also contain a refresh_token in addition to access_token. You can retrive it using getRfreshToken() function. Rrefresh tokens can be used to regenerate the access token when they expire., (*11)

Method 2: Using Personal Access Token.

If you alreay have an active personal access token, you can initialize the API without any credentials, (*12)

use CloudWelder/PetitionsApi/PetitionsApi;

$api = new PetitionsApi();

Making API calls

The API object provides four methods for making API calls. They all return an instance of CloudWelder/PetitionsApi/Response. The returned object contains HTTP response related information such as headers, statsu code and response content. Petitions.io always return response in JSON format. So you must parse the response body to actually use the data. However the ressponse opbjecthas a helper method getResponseData() whcich will return an associative array generated by parsing the response content., (*13)

Before making any API calls, remeber to set the access token using the withToken() method. This method is chainable so you can chain other methods to it., (*14)

The four API call methods are:, (*15)

  • get() Used for making GET request to API end points. Signature: get($url, $data)
    • $url The API end point
    • $data An associative array containing any data to be passed along with the call. Example:
//Get the details of the active user.
$response = $api->withToken($accessToken)->get('users/me');
$userDetails = $response->getResponseData();
  • post() Used for making POST requests to API end points.
    Signature: post($url, $data)
    • $url The API end point
    • $data An associative array containing any data to be passed along with the call. Example:
//Create a new petition.
$petitionData = [
  'title' =>  'My petition',
  'description' => 'This is a sample petition'
];
$response = $api->withToken($accessToken)->post('petitions', $petitionData);
$createdPetition = $response->getResponseData();
  • put() Used for making PUT requests to API end points.
    Signature: put($url, $data)
    • $url The API end point
    • $data An associative array containing any data to be passed along with the call. Example:
//Edit an existing petition
$petitionData = [
  'title' =>  'My new petition'
];
$response = $api->withToken($accessToken)->put('petitions/2', $petitionData);
$changedPetition = $response->getResponseData();
  • delete() Used for making DELETE requests to API end points.
    Signature: put($url)
    • $url The API end point Example:
//Delete a petition
$response = $api->withToken($accessToken)->delete('petitions/2', $petitionData);

The Versions

28/07 2017

dev-master

9999999-dev

This package provides a wrapper for Petitions.io REST Api

  Sources   Download

MIT

The Requires

 

The Development Requires

by Reneesh Tk
by Alwin Tom

api php petitions

30/06 2017

dev-sample-petitions-app

dev-sample-petitions-app

Sample Application for reference

  Sources   Download

MIT

The Requires

 

02/06 2017

0.4

0.4.0.0

This package provides a wrapper for Petitions.io REST Api

  Sources   Download

MIT

The Requires

 

The Development Requires

by Reneesh Tk
by Alwin Tom

api php petitions

31/05 2017

dev-demo-app

dev-demo-app

Sample Application for reference

  Sources   Download

MIT

The Requires

 

30/05 2017

0.3.1

0.3.1.0

This package provides a wrapper for Petitions.io REST Api

  Sources   Download

MIT

The Requires

 

The Development Requires

by Reneesh Tk
by Alwin Tom

api php petitions

25/04 2017

0.3

0.3.0.0

This package provides a wrapper for Petitions.io REST Api

  Sources   Download

MIT

The Requires

 

The Development Requires

by Reneesh Tk
by Alwin Tom

api php petitions

24/04 2017

0.2.0

0.2.0.0

This package provides a wrapper for Petitions.io REST Api

  Sources   Download

MIT

The Requires

 

The Development Requires

by Reneesh Tk
by Alwin Tom

api php petitions

21/04 2017

0.1.5

0.1.5.0

This package provides a wrapper for Petitions.io REST Api

  Sources   Download

MIT

The Requires

 

The Development Requires

by Reneesh Tk
by Alwin Tom

api php petitions

20/04 2017

0.1.4

0.1.4.0

This package provides a wrapper for Petitions.io REST Api

  Sources   Download

MIT

The Requires

 

The Development Requires

by Reneesh Tk
by Alwin Tom

api php petitions

17/04 2017

0.1.3

0.1.3.0

This package provides a wrapper for Petitions.io REST Api

  Sources   Download

MIT

The Requires

 

The Development Requires

by Reneesh Tk
by Alwin Tom

api php petitions

17/04 2017

0.1.2

0.1.2.0

This package provides a wrapper for Petitions.io REST Api

  Sources   Download

MIT

The Requires

 

The Development Requires

by Reneesh Tk
by Alwin Tom

api php petitions

13/04 2017

0.1.1

0.1.1.0

This package provides a wrapper for Petitions.io REST Api

  Sources   Download

MIT

The Requires

 

The Development Requires

by Reneesh Tk
by Alwin Tom

api php petitions

13/04 2017

0.1

0.1.0.0

This package provides a wrapper for Petitions.io REST Api

  Sources   Download

MIT

The Requires

 

The Development Requires

by Reneesh Tk
by Alwin Tom

api php petitions