PHP code example of twodevs / url-shortener

1. Go to this page and download the library: Download twodevs/url-shortener 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/ */

    

twodevs / url-shortener example snippets


    // Create a client
    $client = new \GuzzleHttp\Client(['defaults' => ['verify' => false]]);
    
    // Create the Adapter
    $adapter = new \Ivory\HttpAdapter\GuzzleHttpHttpAdapter($client);
    
    // Create BitlyShortener
    $shorter = new \TwoDevs\UrlShortener\Provider\BitlyProvider($adapter, ['access_token' => 'your-token']));
    
    // Shorten a long url
    $shortUrl = $shorter->shorten('http://example.org');
    
    // Expand a short url
    $longUrl = $shorter->expand($shortUrl);
    
    var_dump( (string) $shortUrl );
    var_dump( (string) $longUrl );

    // Create a client
    $client = new \GuzzleHttp\Client(['defaults' => ['verify' => false]]);
    
    // Create the Adapter
    $adapter = new \Ivory\HttpAdapter\GuzzleHttpHttpAdapter($client);
    
    // Create ChainProvider and attach bitly, google shortener and Tiny-Url
    $shorter  = new \TwoDevs\UrlShortener\Provider\ChainProvider();
    $shorter->addProvider(new \TwoDevs\UrlShortener\Provider\BitlyProvider($adapter, ['access_token' => 'your-token']));
    $shorter->addProvider(new \TwoDevs\UrlShortener\Provider\GoogleProvider($adapter, ['key' => 'your-key']));
    $shorter->addProvider(new \TwoDevs\UrlShortener\Provider\OwlyProvider($adapter, ['key' => 'your-key']));
    $shorter->addProvider(new \TwoDevs\UrlShortener\Provider\TinyUrlProvider($adapter));
    $shorter->addProvider(new \TwoDevs\UrlShortener\Provider\IsgdProvider($adapter));
    $shorter->addProvider(new \TwoDevs\UrlShortener\Provider\VgdUrlProvider($adapter));
    
    // Shorten a long url
    $shortUrl = $shorter->shorten('http://example.org');
    
    // Expand a short url
    $longUrl = $shorter->expand($shortUrl);
    
    var_dump( (string) $shortUrl );
    var_dump( (string) $longUrl );