项目作者: emrahtoy

项目描述 :
PHP URL Shortener and Expander
高级语言: PHP
项目地址: git://github.com/emrahtoy/url-shortener-and-expander.git
创建时间: 2017-04-23T20:51:14Z
项目社区:https://github.com/emrahtoy/url-shortener-and-expander

开源协议:MIT License

下载


PHP URL Shortener and Expander

Url shortener/expander library.

Benefits

  • Produces short codes depending on the (integer) id of the record
  • Supports legacy or custom short urls. So you can move your old system without changing short urls or short codes
  • Uses low server resources
  • Reports errors in json format via responseJson static function
  • Can count redirects ( on Mysql )
  • Supports Doctrine Cache Clients ( example includes redis usage via predis )
  • Uses alphanumeric characters and supports all browsers
  • Uses PDO in order to prevent SQL injection hacks
  • Supports URL check before shorten and after expanded ( checks for http 200 code )
  • Redirection after expand is optional ( redirects with 301 for SEO compliance )
  • You can change allowed characters used in shortened urls code. This may reduce count of possible shortened url code.

Installation

Requirements:

  • PHP
  • Mysql DB or Maria DB
  • PDO Extension must be enabled
  • Credentials will be needed to connect and run SQL queries on database server
  • Sql files under sql directory must be applied on database server
  • Composer

    Install ( via Composer )

  1. $ composer require emrahtoy/url-shortener

Using URL shortener service

  1. $urlShortener = new \UrlCompressor\Shortener($connection, $config);
  2. // returns true or false depending on success and error.
  3. $result = $urlShortener->shorten($url);
  4. // responseJson is a tool to return json with proper headers. This function also redirect with 301 code if you send true as secondary parameter.
  5. // You can send "donotredirect" in order to prevent redirection even it is set "true"
  6. \UrlCompressor\Common::responseJson($urlShortener->getResult(), false);

Using URL expander service

  1. $urlExpander = new \UrlCompressor\Expander($connection, $config);
  2. // returns true or false depending on success and error.
  3. $result = $urlExpander->expand($shortened_code);
  4. // responseJson is a tool to return json with proper headers. This function also redirect with 301 code if you send true as secondary parameter.
  5. // You can send "donotredirect" in order to prevent redirection even it is set "true"
  6. \UrlCompressor\Common::responseJson($urlExpander->getResult(),(isset($_REQUEST['donotredirect']))?false:true);

Available configuration options

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

Using cache

You can use any cache library supported by Doctrine Cache.

This example uses Predis Client with Redis

  1. $urlShortener = new \UrlCompressor\Shortener($connection, $config);
  2. $urlExpander = new \UrlCompressor\Expander($connection, $config);
  3. // using Predis Client with Doctrine Cache
  4. try{
  5. $cacheConfig = [
  6. 'scheme' => 'tcp',
  7. 'host' => '127.0.0.1',
  8. 'port' => 6379,
  9. 'password' => 'supersecretauthentication' // if you have set authentication on redis
  10. ];
  11. //lets create redis client
  12. $cacheClient = new Predis\Client($cacheConfig);
  13. // and try to connect
  14. $cacheClient->connect();
  15. // we can encapsulate cache cilent with doctrine cache if any exception not fired
  16. $cache = new \Doctrine\Common\Cache\PredisCache($cacheClient);
  17. // set cache provider for url shortener service
  18. $urlShortener->setCache($cache);
  19. // set cache provider for url expander service
  20. $urlExpander->setCache($cache);
  21. } catch(Exception $e){
  22. $result=new \UrlCompressor\Result();
  23. $result->error($e->getMessage());
  24. \UrlCompressor\Common::responseJson($result->result());
  25. die();
  26. }