canducci / shorten
Canducci 短网址生成器
v0.0.1
2015-12-19 22:30 UTC
Requires
- php: >=5.3.0
- laravel/framework: 5.1.*|5.0.*
Requires (Dev)
- phpspec/phpspec: ~2.1
- phpunit/phpunit: ~4.0
This package is auto-updated.
Last update: 2024-08-29 04:31:04 UTC
README
##Canducci 短网址生成器
提供商: BitLy, Googl, IsGd, MigreMe, TinyUrl 和 TrIm
该软件包提供最新版本的提供商,可以用于获取网址缩短。包括:
- Bitly (https://bitly.com/)
- Googl (https://developers.google.com/url-shortener/v1/getting_started)
- IsGd (http://is.gd)
- MigreMe (http://migre.me/)
- TinyUrl (http://tinyurl.com/)
- TrIm (https://tr.im/links)
所有这些提供商都以清晰和客观的方式生成网址,当然,您可以选择其中一个或所有来为您的系统生成短网址。
快速开始
所需设置
在 composer.json
文件的 require
键中添加以下内容
"canducci/shorten": "0.0.1"
运行 Composer 更新命令
$ composer update
在您的 config/app.php
中添加 providers
数组
'providers' => [ ..., Canducci\Shorten\Providers\ShortenServiceProvider::class,
在 config/app.php
的末尾添加一个 aliases
(外观)到数组中
'aliases' => [ ..., 'Shorten' => Canducci\Shorten\Facades\Shorten::class, 'IsGd' => Canducci\Shorten\Facades\IsGd::class, 'TinyUrl' => Canducci\Shorten\Facades\TinyUrl::class, 'TrIm' => Canducci\Shorten\Facades\TrIm::class, 'Googl' => Canducci\Shorten\Facades\Googl::class, 'MigreMe' => Canducci\Shorten\Facades\MigreMe::class, 'Bitly' => Canducci\Shorten\Facades\Bitly::class,
如何使用?
使用其中一个提供商(IsGd)的简单示例
$provider = IsGd::create('https://packagist.org.cn/packages/canducci/shorten'); $shorten = Shorten::create($provider); $receive = $shorten->receive(); var_dump($receive); var_dump($receive->getLongUrl()); var_dump($receive->getShortUrl()); var_dump($receive->getProviderType()->getName()); var_dump($receive->getProviderType()->getAddress()); //results object(Canducci\Shorten\ShortenReceive)#225 (3) { ["longurl":protected]=> string(47) "https://packagist.org.cn/packages/canducci/shorten" ["shorturl":protected]=> string(19) "http://is.gd/amftYu" ["providerType":protected]=> object(Canducci\Shorten\ShortenProviderType)#224 (2) { ["name":protected]=> string(4) "IsGd" ["address":protected]=> string(13) "http://is.gd/" } } string(47) "https://packagist.org.cn/packages/canducci/shorten" string(19) "http://is.gd/amftYu" string(4) "IsGd" string(13) "http://is.gd/"
注入契约
Route::get('shorten1', function( Canducci\Shorten\Contracts\IsGdContract $isgd, Canducci\Shorten\Contracts\TinyUrlContract $tinyurl, Canducci\Shorten\Contracts\MigreMeContract $migreme, Canducci\Shorten\Contracts\BitlyContract $bitly, Canducci\Shorten\Contracts\GooglContract $googl, Canducci\Shorten\Contracts\TrImContract $trim, Canducci\Shorten\Contracts\ShortenContract $shorten ) { $provider0 = $isgd->create('https://packagist.org.cn/packages/canducci/shorten'); $provider1 = $tinyurl->create('https://packagist.org.cn/packages/canducci/shorten'); $provider2 = $migreme->create('https://packagist.org.cn/packages/canducci/shorten'); $provider3 = $bitly->create('https://packagist.org.cn/packages/canducci/shorten','key_bitly'); $provider4 = $googl->create('https://packagist.org.cn/packages/canducci/shorten','key_google'); $provider5 = $trim->create('https://packagist.org.cn/packages/canducci/shorten','key_api'); $shorten0 = $shorten->create($provider0); $shorten1 = $shorten->newInstance()->create($provider1); $shorten2 = $shorten->newInstance()->create($provider2); $shorten3 = $shorten->newInstance()->create($provider3); $shorten4 = $shorten->newInstance()->create($provider4); $shorten5 = $shorten->newInstance()->create($provider5); echo $shorten0->receive()->getShortUrl(); echo '<br>'; echo $shorten1->receive()->getShortUrl(); echo '<br>'; echo $shorten2->receive()->getShortUrl(); echo '<br>'; echo $shorten3->receive()->getShortUrl(); echo '<br>'; echo $shorten4->receive()->getShortUrl(); echo '<br>'; echo $shorten5->receive()->getShortUrl(); return; });
外观
$provider0 = IsGd::create('https://packagist.org.cn/packages/canducci/shorten'); $provider1 = TinyUrl::create('https://packagist.org.cn/packages/canducci/shorten'); $provider2 = MigreMe::create('https://packagist.org.cn/packages/canducci/shorten'); $provider3 = Bitly::create('https://packagist.org.cn/packages/canducci/shorten','key_bitly'); $provider4 = Googl::create('https://packagist.org.cn/packages/canducci/shorten','key_google'); $provider5 = TrIm::create('https://packagist.org.cn/packages/canducci/shorten','key_api'); $shorten0 = Shorten::create($provider0); $shorten1 = Shorten::newInstance()->create($provider1); $shorten2 = Shorten::newInstance()->create($provider2); $shorten3 = Shorten::newInstance()->create($provider3); $shorten4 = Shorten::newInstance()->create($provider4); $shorten5 = Shorten::newInstance()->create($provider5); echo $shorten0->receive()->getShortUrl(); echo '<br>'; echo $shorten1->receive()->getShortUrl(); echo '<br>'; echo $shorten2->receive()->getShortUrl(); echo '<br>'; echo $shorten3->receive()->getShortUrl(); echo '<br>'; echo $shorten4->receive()->getShortUrl(); echo '<br>'; echo $shorten5->receive()->getShortUrl();
函数
$provider0 = isgd('https://packagist.org.cn/packages/canducci/shorten'); $provider1 = tinyurl('https://packagist.org.cn/packages/canducci/shorten'); $provider2 = migreme('https://packagist.org.cn/packages/canducci/shorten'); $provider3 = bitly('https://packagist.org.cn/packages/canducci/shorten','key_bitly'); $provider4 = googl('https://packagist.org.cn/packages/canducci/shorten','key_google'); $provider5 = tr_im('https://packagist.org.cn/packages/canducci/shorten','key_api'); $shorten0 = shorten($provider0); $shorten1 = shorten($provider1); $shorten2 = shorten($provider2); $shorten3 = shorten($provider3); $shorten4 = shorten($provider4); $shorten5 = shorten($provider5); echo $shorten0->receive()->getShortUrl(); echo '<br>'; echo $shorten1->receive()->getShortUrl(); echo '<br>'; echo $shorten2->receive()->getShortUrl(); echo '<br>'; echo $shorten3->receive()->getShortUrl(); echo '<br>'; echo $shorten4->receive()->getShortUrl(); echo '<br>'; echo $shorten5->receive()->getShortUrl();
单例
$provider0 = new \Canducci\Shorten\IsGd('https://packagist.org.cn/packages/canducci/shorten'); $provider1 = new \Canducci\Shorten\TinyUrl('https://packagist.org.cn/packages/canducci/shorten'); $provider2 = new \Canducci\Shorten\MigreMe('https://packagist.org.cn/packages/canducci/shorten'); $provider3 = new \Canducci\Shorten\Bitly('https://packagist.org.cn/packages/canducci/shorten','key_bitly'); $provider4 = new \Canducci\Shorten\Googl('https://packagist.org.cn/packages/canducci/shorten','key_google'); $provider5 = new \Canducci\Shorten\TrIm('https://packagist.org.cn/packages/canducci/shorten','key_api'); $shorten0 = new \Canducci\Shorten\Shorten($provider0); $shorten1 = new \Canducci\Shorten\Shorten($provider1); $shorten2 = new \Canducci\Shorten\Shorten($provider2); $shorten3 = new \Canducci\Shorten\Shorten($provider3); $shorten4 = new \Canducci\Shorten\Shorten($provider4); $shorten5 = new \Canducci\Shorten\Shorten($provider5); echo $shorten0->receive()->getShortUrl(); echo '<br>'; echo $shorten1->receive()->getShortUrl(); echo '<br>'; echo $shorten2->receive()->getShortUrl(); echo '<br>'; echo $shorten3->receive()->getShortUrl(); echo '<br>'; echo $shorten4->receive()->getShortUrl(); echo '<br>'; echo $shorten5->receive()->getShortUrl();
注意
以下三个提供商必须在同一网站上注册,才能使用提供的令牌或密钥生成短网址。
- Bitly (key_bitly) (https://bitly.com/)
- Googl (key_google) (https://developers.google.com/url-shortener/v1/getting_started)
- TrIm (key_api) (https://tr.im/links)
与其他不同,只需要提供网址信息即可成功运行代码。