arirah-shortener/url-shortener

流行的URL缩短服务提供商的URL缩短服务

dev-master 2020-01-11 08:31 UTC

This package is auto-updated.

Last update: 2024-09-11 18:54:01 UTC


README

URL缩短包具有支持不同URL缩短服务提供商的能力。

安装

使用以下命令安装composer依赖项:docker run --rm -v $(pwd):/app composer install

您也可以运行make命令,例如:make init

运行示例:docker run --rm -v $(pwd):/app -w /app php:cli php examples/bitly.php

Docker phpunit测试命令:docker run --rm -v $(pwd):/app -w /app php:cli php ./vendor/bin/phpunit tests/urlTest

使用包管理器composer(https://getcomposer.org.cn/doc/04-schema.md)安装URL缩短。

composer require arirah-shortener/url-shortener:dev-master

用法

提供者

目前我们有bitly和tinyurl两个提供者,更多将很快添加。

TinyUrl

在你的文件中使用如下,你必须设置提供者实例

$shortener =  new UrlShortener(new \ArirahShortener\UrlShortener\TinyUrl());
Bitly

对于Bitly,你需要设置API访问_token

$shortener =  new UrlShortener(new \ArirahShortener\UrlShortener\Bitly('f7bb93c3ae74d10db0de48e9e038f13000e07d05'));

目前我们有bitly和tiny url两个提供者。默认提供者是tinyurl。对于bit.ly,你必须从bitly账户设置访问_token(https://bitly.com/a/oauth_apps)。没有access_token?不用担心,它会自动切换到tinyurl提供者。

示例

#BitLy
try {
    $access_token = new ArirahShortener\UrlShortener\AccessToken('f7bb93c3ae74d10db0de48e9e038f13000e07d05');
    $shortener =  new UrlShortener(new ArirahShortener\UrlShortener\Bitly($access_token));
    echo (string) $shortener->shorten('https://news.google.com/?hl=en-US&gl=US&ceid=US:en');
} catch (Exception $e) {
    echo $e->getMessage() ;
}

# TinyURL
try {
    $shortener =  new UrlShortener();
    echo (string) $shortener->shorten('https://news.google.com/?hl=en-US&gl=US&ceid=US:en');
} catch (Exception $e) {
    echo $e->getMessage() ;
}

返回:http://bit.ly/39Q2JoZ

贡献

欢迎pull requests。对于主要更改,请先打开一个问题来讨论您想更改的内容。

请确保适当更新测试。