mremi/url-shortener

A PHP5 library using API to shorten/expand URL

Installs: 7 531

Dependents: 1

Stars: 10

Watchers: 2

Forks: 3

Language: PHP

v1.0.3 2013-10-18 21:34 UTC

README

This library allows you to shorten a URL, reverse is also possible.

SensioLabsInsight

Build Status Total Downloads Latest Stable Version Scrutinizer Quality Score Code Coverage

Basic Docs

Installation

Only 1 step:

Download UrlShortener using composer

Add UrlShortener in your composer.json:

{
    "require": {
        "mremi/url-shortener": "dev-master"
    }
}

Now tell composer to download the library by running the command:

$ php composer.phar update mremi/url-shortener

Composer will install the library to your project's vendor/mremi directory.

Bit.ly API

Shorten

<?php

use Mremi\UrlShortener\Model\Link;
use Mremi\UrlShortener\Provider\Bitly\BitlyProvider;
use Mremi\UrlShortener\Provider\Bitly\OAuthClient;

$link = new Link;
$link->setLongUrl('http://www.google.com');

$bitlyProvider = new BitlyProvider(
    new OAuthClient('username', 'password'),
    array('connect_timeout' => 1, 'timeout' => 1)
);

$bitlyProvider->shorten($link);

You can also use the commands provided by this library, look at the help message:

$ bin/shortener bitly:shorten --help

Some arguments are mandatory:

$ bin/shortener bitly:shorten username password http://www.google.com

Some options are available:

$ bin/shortener bitly:shorten username password http://www.google.com --options='{"connect_timeout":1,"timeout":1}'

Expand

<?php

use Mremi\UrlShortener\Model\Link;
use Mremi\UrlShortener\Provider\Bitly\BitlyProvider;
use Mremi\UrlShortener\Provider\Bitly\OAuthClient;

$link = new Link;
$link->setLongUrl('http://www.google.com');

$bitlyProvider = new BitlyProvider(
    new OAuthClient('username', 'password'),
    array('connect_timeout' => 1, 'timeout' => 1)
);

$bitlyProvider->expand($link);
$ bin/shortener bitly:expand --help

Some arguments are mandatory:

$ bin/shortener bitly:expand username password http://bit.ly/ze6poY

Some options are available:

$ bin/shortener bitly:expand username password http://bit.ly/ze6poY --options='{"connect_timeout":1,"timeout":1}'

Google API

Shorten

<?php

use Mremi\UrlShortener\Model\Link;
use Mremi\UrlShortener\Provider\Google\GoogleProvider;

$link = new Link;
$link->setLongUrl('http://www.google.com');

$googleProvider = new GoogleProvider(
    'api_key',
    array('connect_timeout' => 1, 'timeout' => 1)
);

$googleProvider->shorten($link);

You can also use the commands provided by this library, look at the help message:

$ bin/shortener google:shorten --help

Only one argument is mandatory (the long URL) but you can also provide a Google API key:

$ bin/shortener google:shorten http://www.google.com
$ bin/shortener google:shorten http://www.google.com api_key

Some options are available:

$ bin/shortener google:shorten http://www.google.com --options='{"connect_timeout":1,"timeout":1}'

Expand

<?php

use Mremi\UrlShortener\Model\Link;
use Mremi\UrlShortener\Provider\Google\GoogleProvider;

$link = new Link;
$link->setLongUrl('http://www.google.com');

$googleProvider = new GoogleProvider(
    'api_key',
    array('connect_timeout' => 1, 'timeout' => 1)
);

$googleProvider->expand($link);
$ bin/shortener google:expand --help

Only one argument is mandatory (the short URL) but you can also provide a Google API key:

$ bin/shortener google:expand http://goo.gl/fbsS
$ bin/shortener google:expand http://goo.gl/fbsS api_key

Some options are available:

$ bin/shortener google:expand http://goo.gl/fbsS --options='{"connect_timeout":1,"timeout":1}'

Chain providers

<?php

use Mremi\UrlShortener\Model\Link;
use Mremi\UrlShortener\Provider\ChainProvider;

$chainProvider = new ChainProvider;
$chainProvider->addProvider($bitlyProvider);
$chainProvider->addProvider($googleProvider);
// add yours...

$link = new Link;
$link->setLongUrl('http://www.google.com');

$chainProvider->getProvider('bitly')->shorten($link);

$chainProvider->getProvider('google')->expand($link);

Retrieve link

You can retrieve some links using these finders:

<?php

use Mremi\UrlShortener\Model\LinkManager;

$linkManager = new LinkManager($chainProvider);

$shortened = $linkManager->findOneByProviderAndShortUrl('bitly', 'http://bit.ly/ze6poY');

$expanded = $linkManager->findOneByProviderAndLongUrl('google', 'http://www.google.com');

Contribution

Any question or feedback? Open an issue and I will try to reply quickly.

A feature is missing here? Feel free to create a pull request to solve it!

I hope this has been useful and has helped you. If so, share it and recommend it! :)

@mremitsme