emrahtoy/url-shortener-and-expander

1.0.3 2017-04-29 07:37 UTC

This package is not auto-updated.

Last update: 2024-09-25 01:22:14 UTC


README

URL 短链/展开库。

优势

  • 根据记录的 (整数) id 生成短码
  • 支持旧版或自定义短 URL。因此,您可以在不更改短 URL 或短码的情况下迁移旧系统
  • 使用低服务器资源
  • 通过 responseJson 静态函数以 JSON 格式报告错误
  • 可以计算重定向 (在 MySQL 上)
  • 支持 Doctrine 缓存客户端(例如,通过 predis 使用 Redis)
  • 使用字母数字字符并支持所有浏览器
  • 使用 PDO 防止 SQL 注入攻击
  • 在缩短和展开 URL 前后支持 URL 检查(检查 HTTP 200 状态码)
  • 展开后的重定向是可选的(使用 301 重定向以符合 SEO 规范)
  • 您可以更改用于缩短 URL 代码中的允许字符。这可能会减少可能的缩短 URL 代码的数量

安装

要求

  • PHP
  • MySQL 数据库或 MariaDB
  • PDO 扩展必须启用
  • 需要凭证以连接数据库服务器并运行 SQL 查询
  • sql 目录下的 SQL 文件必须在数据库服务器上应用
  • Composer

安装(通过 Composer)

$ composer require emrahtoy/url-shortener

使用 URL 短链服务

$urlShortener = new \UrlCompressor\Shortener($connection, $config);

// returns true or false depending on success and error.
$result = $urlShortener->shorten($url); 

// responseJson is a tool to return json with proper headers. This function also redirect with 301 code if you send true as secondary parameter.
// You can send "donotredirect" in order to prevent redirection even it is set "true"
\UrlCompressor\Common::responseJson($urlShortener->getResult(), false);

使用 URL 展开服务

$urlExpander = new \UrlCompressor\Expander($connection, $config);

// returns true or false depending on success and error.
$result = $urlExpander->expand($shortened_code); 

// responseJson is a tool to return json with proper headers. This function also redirect with 301 code if you send true as secondary parameter.
// You can send "donotredirect" in order to prevent redirection even it is set "true" 
\UrlCompressor\Common::responseJson($urlExpander->getResult(),(isset($_REQUEST['donotredirect']))?false:true);

可用的配置选项

$config = [
    'CheckUrl' => false, // check url before shortening or after expand
    'ShortUrlTableName' => 'shortenedurls', // Database table name where shortened url codes are stored
    'CustomShortUrlTableName' => 'customshortenedurls', // Database table name where your legacy shortened codes are stored
    'TrackTableName' => 'track', // Database table name where the visit/redirect counts are stored
    'DefaultLocation' => '', // where to redirect if expanded url could not find
    'Track'=>false // Determines if visit/redirects will be stored
];

使用缓存

您可以使用 Doctrine 缓存 支持的任何缓存库。

此示例使用 Predis 客户端Redis

$urlShortener = new \UrlCompressor\Shortener($connection, $config);
$urlExpander = new \UrlCompressor\Expander($connection, $config);

// using Predis Client with Doctrine Cache
try{
    $cacheConfig = [
        'scheme' => 'tcp',
        'host' => '127.0.0.1',
        'port' => 6379,
        'password' => 'supersecretauthentication' // if you have set authentication on redis
    ];
    
    //lets create redis client
    $cacheClient = new Predis\Client($cacheConfig);
    // and try to connect
    $cacheClient->connect();
    
    // we can encapsulate cache cilent with doctrine cache if any exception not fired 
    $cache = new \Doctrine\Common\Cache\PredisCache($cacheClient);

    // set cache provider for url shortener service 
    $urlShortener->setCache($cache);
    
    // set cache provider for url expander service 
    $urlExpander->setCache($cache);
    
} catch(Exception $e){
    $result=new \UrlCompressor\Result();
    $result->error($e->getMessage());
    \UrlCompressor\Common::responseJson($result->result());
    die();
}