UrlShortener
A PHP library to generate shortened URL through famous API like Bit.ly or Google
Install / Use
/learn @mremi/UrlShortenerREADME
URL shortener library
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
<a name="installation"></a>
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
<a name="baidu-api"></a>
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}'
<a name="bitly-api"></a>
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}'
<a name="google-api"></a>
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}'
<a name="sina-api"></a>
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}'
<a name="wechat-api"></a>
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.
<a name="chain-providers"></a>
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);
<a name="retrieve-link"></a>
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');
<a name="contribution"></a>
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! :)
Related Skills
node-connect
351.2kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
110.6kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
351.2kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
351.2kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。






