PHP code example of emrahtoy / url-shortener-and-expander

1. Go to this page and download the library: Download emrahtoy/url-shortener-and-expander library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

emrahtoy / url-shortener-and-expander example snippets


$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);

$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
];

$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();
}