URL shortener library
This library allows you to shorten a URL, reverse is also possible., (*1)
, (*2)
, (*3)
Basic Docs, (*4)
, (*5)
Installation
The preferred method of installation is via Composer. Run the following
command to install the package and add it as a requirement to your project's
composer.json
:, (*6)
composer require mremi/url-shortener
, (*7)
Baidu API
Shorten
<?php
use Mremi\UrlShortener\Model\Link;
use Mremi\UrlShortener\Provider\Baidu\BaiduProvider;
$link = new Link;
$link->setLongUrl('http://www.google.com');
$baiduProvider = new BaiduProvider(
['connect_timeout' => 1, 'timeout' => 1]
);
$baiduProvider->shorten($link);
You can also use the commands provided by this library, look at the help message:, (*8)
$ bin/shortener baidu:shorten --help
$ bin/shortener baidu:shorten http://www.google.com
Some options are available:, (*9)
$ bin/shortener baidu:shorten http://www.google.com --options='{"connect_timeout":1,"timeout":1}'
Expand
<?php
use Mremi\UrlShortener\Model\Link;
use Mremi\UrlShortener\Provider\Baidu\BaiduProvider;
$link = new Link;
$link->setShortUrl('http://dwz.cn/dDlVEAt5');
$googleProvider = new BaiduProvider(
['connect_timeout' => 1, 'timeout' => 1]
);
$googleProvider->expand($link);
$ bin/shortener baidu:expand --help
$ bin/shortener baidu:expand http://dwz.cn/dDlVEAt5
Some options are available:, (*10)
$ bin/shortener baidu:expand http://dwz.cn/dDlVEAt5 --options='{"connect_timeout":1,"timeout":1}'
, (*11)
Bit.ly API V4
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 GenericAccessTokenAuthenticator('generic_access_token'), // or old OAuthClient('username', 'password')
['connect_timeout' => 1, 'timeout' => 1]
);
$bitlyProvider->shorten($link);
You can also use the commands provided by this library, look at the help message:, (*12)
$ bin/shortener bitly:shorten --help
Some arguments are mandatory:, (*13)
$ bin/shortener bitly:shorten username password http://www.google.com
Some options are available:, (*14)
$ 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->setShortUrl('http://goo.gl/fbsS');
$bitlyProvider = new BitlyProvider(
new GenericAccessTokenAuthenticator('generic_access_token'), // or old OAuthClient('username', 'password')
['connect_timeout' => 1, 'timeout' => 1]
);
$bitlyProvider->expand($link);
$ bin/shortener bitly:expand --help
Some arguments are mandatory:, (*15)
$ bin/shortener bitly:expand username password http://bit.ly/ze6poY
Some options are available:, (*16)
$ bin/shortener bitly:expand username password http://bit.ly/ze6poY --options='{"connect_timeout":1,"timeout":1}'
, (*17)
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',
['connect_timeout' => 1, 'timeout' => 1]
);
$googleProvider->shorten($link);
You can also use the commands provided by this library, look at the help message:, (*18)
$ bin/shortener google:shorten --help
Only one argument is mandatory (the long URL) but you can also provide a Google
API key:, (*19)
$ bin/shortener google:shorten http://www.google.com
$ bin/shortener google:shorten http://www.google.com api_key
Some options are available:, (*20)
$ 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->setShortUrl('http://goo.gl/fbsS');
$googleProvider = new GoogleProvider(
'api_key',
['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:, (*21)
$ bin/shortener google:expand http://goo.gl/fbsS
$ bin/shortener google:expand http://goo.gl/fbsS api_key
Some options are available:, (*22)
$ bin/shortener google:expand http://goo.gl/fbsS --options='{"connect_timeout":1,"timeout":1}'
, (*23)
Sina API
Shorten
<?php
use Mremi\UrlShortener\Model\Link;
use Mremi\UrlShortener\Provider\Sina\SinaProvider;
$link = new Link;
$link->setLongUrl('http://www.google.com');
$sinaProvider = new SinaProvider(
'api_key',
['connect_timeout' => 1, 'timeout' => 1]
);
$sinaProvider->shorten($link);
You can also use the commands provided by this library, look at the help message:, (*24)
$ bin/shortener sina:shorten --help
$ bin/shortener sina:shorten http://www.google.com api_key
Some options are available:, (*25)
$ bin/shortener sina:shorten http://www.google.com api_key --options='{"connect_timeout":1,"timeout":1}'
Expand
<?php
use Mremi\UrlShortener\Model\Link;
use Mremi\UrlShortener\Provider\Sina\SinaProvider;
$link = new Link;
$link->setShortUrl('http://t.cn/h51yw');
$sinaProvider = new SinaProvider(
'api_key',
['connect_timeout' => 1, 'timeout' => 1]
);
$googleProvider->expand($link);
$ bin/shortener sina:expand --help
$ bin/shortener sina:expand http://t.cn/h51yw api_key
Some options are available:, (*26)
$ bin/shortener sina:expand http://t.cn/h51yw api_key --options='{"connect_timeout":1,"timeout":1}'
, (*27)
Wechat API
Shorten
<?php
use Mremi\UrlShortener\Model\Link;
use Mremi\UrlShortener\Provider\Wechat\WechatProvider;
use Mremi\UrlShortener\Provider\Wechat\OAuthClient;
$link = new Link;
$link->setLongUrl('http://www.google.com');
$wechatProvider = new WechatProvider(
new OAuthClient('username', 'password'),
['connect_timeout' => 1, 'timeout' => 1]
);
$wechatProvider->shorten($link);
You can also use the commands provided by this library, look at the help message:, (*28)
$ bin/shortener wechat:shorten --help
Some arguments are mandatory:, (*29)
$ bin/shortener wechat:shorten appid secret http://www.google.com
Some options are available:, (*30)
$ bin/shortener wechat:shorten appid secret http://www.google.com --options='{"connect_timeout":1,"timeout":1}'
Expand
Wechat does not support expand url yet., (*31)
, (*32)
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);
, (*33)
Retrieve link
You can retrieve some links using these finders:, (*34)
<?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');
, (*35)
Contribution
Any question or feedback? Open an issue and I will try to reply quickly., (*36)
A feature is missing here? Feel free to create a pull request to solve it!, (*37)
I hope this has been useful and has helped you. If so, share it and recommend
it! :), (*38)
@mremitsme, (*39)