pixxet/url-shortener

A PHP5 library using API to shorten/expand URL

v2.1.1 2018-02-27 15:01 UTC

This package is auto-updated.

Last update: 2024-11-08 07:13:06 UTC


README

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

SensioLabsInsight

Build Status Total Downloads Latest Stable Version

Basic Docs

Installation

Only 1 step:

Download UrlShortener using composer

Add UrlShortener in your composer.json:

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

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

$ php composer.phar update pixxet/url-shortener

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

Bit.ly API

Shorten

<?php

use Pixxet\UrlShortener\Model\Link;
use Pixxet\UrlShortener\Provider\Bitly\BitlyProvider;
use Pixxet\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}'

Expand

<?php

use Pixxet\UrlShortener\Model\Link;
use Pixxet\UrlShortener\Provider\Bitly\BitlyProvider;
use Pixxet\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}'

Google API

Shorten

<?php

use Pixxet\UrlShortener\Model\Link;
use Pixxet\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 Pixxet\UrlShortener\Model\Link;
use Pixxet\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}'

Chain providers

<?php

use Pixxet\UrlShortener\Model\Link;
use Pixxet\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 Pixxet\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! :)

Credentials @mremitsme

@pixxetde