mremi / url-shortener
A PHP5/PHP7/PHP8 library using API to shorten/expand URL
Installs: 670 174
Dependents: 8
Suggesters: 0
Security: 0
Stars: 79
Watchers: 5
Forks: 32
Open Issues: 6
Requires
- php: ^5.6 || ^7.0 || ^8.0
- guzzlehttp/guzzle: ^6.0 || ^7.0
- symfony/console: ^3.1 || ^4.1 || ^5.0 || ^6.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.19
- symfony/phpunit-bridge: ^5.0 || ^6.0
README
This library allows you to shorten a URL, reverse is also possible.
Basic Docs
- Installation
- Baidu API
- Bit.ly API
- Google API
- Sina API
- Wechat API
- Chain providers
- Retrieve link
- Contribution
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
:
composer require mremi/url-shortener
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:
$ bin/shortener baidu:shorten --help
$ bin/shortener baidu:shorten http://www.google.com
Some options are available:
$ 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:
$ bin/shortener baidu:expand http://dwz.cn/dDlVEAt5 --options='{"connect_timeout":1,"timeout":1}'
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:
$ 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->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:
$ 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', ['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->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:
$ 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}'
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:
$ bin/shortener sina:shorten --help
$ bin/shortener sina:shorten http://www.google.com api_key
Some options are available:
$ 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:
$ bin/shortener sina:expand http://t.cn/h51yw api_key --options='{"connect_timeout":1,"timeout":1}'
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:
$ bin/shortener wechat:shorten --help
Some arguments are mandatory:
$ bin/shortener wechat:shorten appid secret http://www.google.com
Some options are available:
$ bin/shortener wechat:shorten appid secret http://www.google.com --options='{"connect_timeout":1,"timeout":1}'
Expand
Wechat does not support expand url yet.
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! :)