Download the PHP package cloudwelder/petitions-api without Composer
On this page you can find all versions of the php package cloudwelder/petitions-api. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download cloudwelder/petitions-api
More information about cloudwelder/petitions-api
Files in cloudwelder/petitions-api
Package petitions-api
Short Description This package provides a wrapper for Petitions.io REST Api
License MIT
Informations about the package petitions-api
Petitions.io API PHP Wrapper
This is PHP wrapper to Petitions.io API
Contents
- Installation
- Pre-requisites
- Usage
- Initializing the API
- Method 1: Using Credentials
- Method 2: Using Personal Access Token
- Generating Login URL
- Acquring Access Token
- Making API Calls
Installation
Installation is done using composer. Run the following command to include this package into your project
composer require cloudwelder/petitions-api
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.
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.
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.
Methods 1: Using API credentials.
Substitute client_id
, client_secret
, redirect_uri
with your own values.
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.
You can then use this url to reidrect the user or as the href
of an html link.
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.
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)
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.
Method 2: Using Personal Access Token.
If you alreay have an active personal access token, you can initialize the API without any credentials
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.
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.
The four API call methods are:
-
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:
-
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:
-
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:
delete()
Used for making DELETE requests to API end points.
Signature:put($url)
$url
The API end point Example: