This library allows you to shorten a URL, reverse is also possible.
Basic Docs
Only 1 step:
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.
<?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'), // or new GenericAccessTokenAuthenticator('generic_access_token')
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}'
<?php
use Mremi\UrlShortener\Model\Link;
use Mremi\UrlShortener\Provider\Bitly\BitlyProvider;
use Mremi\UrlShortener\Provider\Bitly\OAuthClient;
$link = new Link;
$link->setShortUrl('http://goo.gl/fbsS');
$bitlyProvider = new BitlyProvider(
new OAuthClient('username', 'password'), // or new GenericAccessTokenAuthenticator('generic_access_token')
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}'
<?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}'
<?php
use Mremi\UrlShortener\Model\Link;
use Mremi\UrlShortener\Provider\Google\GoogleProvider;
$link = new Link;
$link->setShortUrl('http://goo.gl/fbsS');
$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}'
<?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);
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');
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! :)