Snip.ly PHP Utilities
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src
.gitignore
LICENSE
README.md
composer.json

README.md

Snip.ly PHP utilities

Description

Tools for Snip.ly's API

Install

Via Composer

{
    "require": {
        "younes0/sniply": "dev-master"
    }
}

Requires PHP 5.4+

Usage

OAuth2

$provider = new Younes0\Sniply\OAuth2Provider([
	'clientId'     => 'XXXXXXXX',
	'clientSecret' => 'XXXXXXXX',
	'redirectUri'  => 'https://your-registered-redirect-uri/'
]);

if ( ! isset($_GET['code'])) {

    // If we don't have an authorization code then get one
    header('Location: '.$provider->getAuthorizationUrl());

} else {

	// Try to get an access token (using the authorization code grant)
    $token = $provider->getAccessToken('authorizationCode', [
    	'code' => $_GET['code']
    ]);

    $accessToken = $token->accessToken;

    echo $accessToken;
}

API Wrapper

API methods: http://snip.ly/api/

$sniply = new Younes0\Sniply\Client($accessToken);

$sniply->fetch() // Fetch All Snips Created By User
$sniply->fetch('foO') // Get A Specific Snip

$sniply->create($link, $message); //  Create a new snip
$sniply->edit('foO', $link, $message); // Edit a snip

// optional parameters for Pro plans
$sniply->create($link, $message, $optional = [
    'background_color' => '#fff',
    'message_color'    => '#000',
    'theme'            => 'fullwidth',
    'show_sniply_logo' => 'true', // string
    'button_action'    => [
        'text'             => $text,
        'url'              => $url,
        'background_color' => '#ff0000',
        'text_color'       => '#fff',
    ],
]);

// fetch all profiles & edit first one
$profiles = $sniply->fetchProfiles();
$sniply->editProfile($profiles[0]['id'], $name = 'Younès', $optional = [
    'website' => 'http://www.github.com/younes0'
]);

You can access to Guzzle Response object instead of body by setting $body to false (usually the last parameter, check the source)

// outputs guzzle response body as array (parsed json)
$response = $sniply->create($link, $message); 

// ouputs guzzle response object
$response = $sniply->create($link, $message, $optional, false);
$response->getReasonPhrase(); // 'CREATED'
echo $response->getBody(); // json

More infos at: http://guzzle.readthedocs.org/en/latest/http-messages.html#body

Finally, you can set a custom Guzzle Client which is useful when you need to mock responses or set a retry subscriber

$mockAdapter = new MockAdapter(function (TransactionInterface $trans) {
    $request = $trans->getRequest();
    return new Response(200);
});

$sniply->setGuzzleClient(new \Guzzle\Client(['adapter' => $mockAdapter]));

More infos at: http://guzzle.readthedocs.org/en/latest/testing.html#mock-adapter