Coder Social home page Coder Social logo

infogram-php's Introduction

#Infogram-PHP

This library provides an API to create and update infographics on Infogr.am

Installation

The recommended way to install Infogram-PHP is via Composer. Composer is a tool for dependency management in PHP. It allows you to declare the dependent libraries your project needs and it will install them in your project for you.

# Install Composer
curl -sS https://getcomposer.org/installer | php

Add the following dependency to the require section of your project's composer.json:

"require": {
  "infogram/infogram": "1.0.*"
}

Execute command line composer update in your project's root

API Keys

You're going to need API keys, the public key and the secret (or private) key, to be able to access infogr.am API service. To find your keys, log in to Infogr.am, find the account settings on the left panel and look for the API credentials. Keep your secret key secret, don't send it to anyone and do not pass it to any service directly.

The public key is used to indentify the API account the code is accessing with and the secret key is used to sign every request, i.e., add additional parameter to the request query string or body.

Usage

Making a Request

For making a request, two Infogram-PHP classes are essential: Infogram\RequestSigningSession and Infogram\InfogramRequest. The former is responsible for HTTP request signing using the provided API secret key and the latter performs HTTP requests to the Infogr.am API service.

TODO: add reference to HTTP API documentation

use Infogram\InfogramRequest;
use Infogram\RequestSigningSession;
//...
$consumerKey = 'Your public key';
$consumerSecret = 'Your secret key';
$session = new RequestSigningSession($consumerKey, $consumerSecret);
$request = new InfogramRequest($session, 'GET', 'themes');
$response = $request->execute();

Using a Response

Method Infogram\InfogramRequest::execute returns an instance of class Infogram\InfogramResponse or null if Infogr.am API server cannot be accessed.

Infogram\InfogramResponse contains (1) HTTP status code from last API request, (2) HTTP response body, (3) HTTP response headers

$content = array(
  array(
    'type' => 'h1',
    'text' => 'hello'
  )
);
$session = new RequestSigningSession($consumerKey, $consumerSecret);
$request = new InfogramRequest($session, 'POST', 'infographics', array('content' => $content));
$response = $request->execute();
if (! $response) {
   die("Could not contact Infogr.am API server\n");
}
if (! $response->isOK()) {
   die('Error executing API request: ' . $response->getBody() . "\n");
}
echo 'Created new infographic with ID: ' . $response->getHeader('X-Infogram-Id') . "\n";

On successful request ($response->isOK() == true), response's method getBody returns either a string or array (converted from a JSON string) if applicable. On error (! $response->isOK()), getBody returns string which contains the error message if there is any.

infogram-php's People

Contributors

torbjon avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.