PHP code example of pixxet / url-shortener

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

    

pixxet / url-shortener example snippets




use Pixxet\UrlShortener\Model\Link;
use Pixxet\UrlShortener\Provider\Bitly\BitlyProvider;
use Pixxet\UrlShortener\Provider\Bitly\OAuthClient;

$link = new Link;
$link->setLongUrl('http://www.google.com');

$bitlyProvider = new BitlyProvider(
    new OAuthClient('username', 'password'), // or new GenericAccessTokenAuthenticator('generic_access_token')
    array('connect_timeout' => 1, 'timeout' => 1)
);

$bitlyProvider->shorten($link);



use Pixxet\UrlShortener\Model\Link;
use Pixxet\UrlShortener\Provider\Bitly\BitlyProvider;
use Pixxet\UrlShortener\Provider\Bitly\OAuthClient;

$link = new Link;
$link->setShortUrl('http://goo.gl/fbsS');

$bitlyProvider = new BitlyProvider(
    new OAuthClient('username', 'password'), // or new GenericAccessTokenAuthenticator('generic_access_token')
    array('connect_timeout' => 1, 'timeout' => 1)
);

$bitlyProvider->expand($link);



use Pixxet\UrlShortener\Model\Link;
use Pixxet\UrlShortener\Provider\Google\GoogleProvider;

$link = new Link;
$link->setLongUrl('http://www.google.com');

$googleProvider = new GoogleProvider(
    'api_key',
    array('connect_timeout' => 1, 'timeout' => 1)
);

$googleProvider->shorten($link);



use Pixxet\UrlShortener\Model\Link;
use Pixxet\UrlShortener\Provider\Google\GoogleProvider;

$link = new Link;
$link->setShortUrl('http://goo.gl/fbsS');

$googleProvider = new GoogleProvider(
    'api_key',
    array('connect_timeout' => 1, 'timeout' => 1)
);

$googleProvider->expand($link);



use Pixxet\UrlShortener\Model\Link;
use Pixxet\UrlShortener\Provider\ChainProvider;

$chainProvider = new ChainProvider;
$chainProvider->addProvider($bitlyProvider);
$chainProvider->addProvider($googleProvider);
// add yours...

$link = new Link;
$link->setLongUrl('http://www.google.com');

$chainProvider->getProvider('bitly')->shorten($link);

$chainProvider->getProvider('google')->expand($link);



use Pixxet\UrlShortener\Model\LinkManager;

$linkManager = new LinkManager($chainProvider);

$shortened = $linkManager->findOneByProviderAndShortUrl('bitly', 'http://bit.ly/ze6poY');

$expanded = $linkManager->findOneByProviderAndLongUrl('google', 'http://www.google.com');
 bash
$ php composer.phar update pixxet/url-shortener