mechpave/imgur-client

Imgur API wrapper

0.3.0 2016-01-03 09:25 UTC

This package is not auto-updated.

Last update: 2021-05-15 01:25:32 UTC


README

Build Status Scrutinizer Code Quality Code Coverage Build Status

This package provides an object oriented wrapper for v3 Imgur API Documentation about Imgur API can be found at api.imgur.com Basic usage examples are provided in this readme file. More detailed documentation can be found in the docs folder

Installation

The best way to install and use ImgurClient is using Composer

composer require mechpave/imgur-client

Basic usage

Authorization

Detailed documentation is provided in the authorization docs section.

In order to be able to access Imgur API, firstly, you have to register your app and receive client ID and client secret. The you can use those values in your project.

// This file is generated by Composer
require_once 'vendor/autoload.php';

$clientId = 'id';
$clientSecret = 'secret';
$client = new \Mechpave\ImgurClient\ImgurClient($clientId, $clientSecret);

This is sufficient if you need to only access anonymous and publicly accessible content If you want to access users private data, you hve to get permission from the user.

If you already have access token, create a token object and set it to client.

$token = new \Mechpave\ImgurClient\Entity\Token();
$token->setAccessToken('access_token');
$token->setRefreshToken('refresh_token');

$client->setToken($token);

If you do not yet have the access token, you must redirect the user to imgur authorization page. Once the user grants permission for your app to access his account, you will get the access token. There are several ways to get the access token. Authorization docs section describes that in more details.

//get Authorization handler
$authHandler = $client->getAuthorizationHandler();

//Get the URI to the authorization page and point user to this page
$url = $authHandler->getAuthorizationUrl();

//If you use PIN, or CODE authorization types, you have to exchange the code for access token:
$token = $authHandler->requestAccessToken('code received after user has granted permission for your app');

Once you receive the access token, you should save it for later use. Set it to client as described above.

Using API endpoints

Documentation about all the endpoints is provided in docs/endpoints folder This readme file provides only basic example of using Image endpoint:

Using Image API endpoint

Get image information:

$image = $client->image()->get('image_id');

The client parses the imgur.com response and maps its data to an Image object. So, you get an Image object, rather that an array and you are able to work with it in object oriented way:

$title = $image->getTitle();
$id = $image->getImageId();

In your project, sometimes you will probably want to use your own objects (in order to save them in the database, etc.). It is possible to configure Imgur API client to map the response data to your object, instead of the built in one. You can read more on that in documentation.

Uploading an image:

$image = $client->image()->upload(
    'path/to/image/file',
    \Mechpave\ImgurClient\Model\ImageModel::TYPE_FILE,
    'title',
    'description'
);
$iamgeId = $image->getImageId();