PHP code example of mremi / url-shortener

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

    

mremi / url-shortener example snippets




use Mremi\UrlShortener\Model\Link;
use Mremi\UrlShortener\Provider\Baidu\BaiduProvider;

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

$baiduProvider = new BaiduProvider(
    ['connect_timeout' => 1, 'timeout' => 1]
);

$baiduProvider->shorten($link);



use Mremi\UrlShortener\Model\Link;
use Mremi\UrlShortener\Provider\Baidu\BaiduProvider;

$link = new Link;
$link->setShortUrl('http://dwz.cn/dDlVEAt5');

$googleProvider = new BaiduProvider(
    ['connect_timeout' => 1, 'timeout' => 1]
);

$googleProvider->expand($link);



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

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

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

$bitlyProvider->shorten($link);



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

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

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

$bitlyProvider->expand($link);



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

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

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

$googleProvider->shorten($link);



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

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

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

$googleProvider->expand($link);



use Mremi\UrlShortener\Model\Link;
use Mremi\UrlShortener\Provider\Sina\SinaProvider;

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

$sinaProvider = new SinaProvider(
    'api_key',
    ['connect_timeout' => 1, 'timeout' => 1]
);

$sinaProvider->shorten($link);



use Mremi\UrlShortener\Model\Link;
use Mremi\UrlShortener\Provider\Sina\SinaProvider;

$link = new Link;
$link->setShortUrl('http://t.cn/h51yw');

$sinaProvider = new SinaProvider(
    'api_key',
    ['connect_timeout' => 1, 'timeout' => 1]
);

$googleProvider->expand($link);



use Mremi\UrlShortener\Model\Link;
use Mremi\UrlShortener\Provider\Wechat\WechatProvider;
use Mremi\UrlShortener\Provider\Wechat\OAuthClient;

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

$wechatProvider = new WechatProvider(
    new OAuthClient('username', 'password'),
    ['connect_timeout' => 1, 'timeout' => 1]
);

$wechatProvider->shorten($link);



use Mremi\UrlShortener\Model\Link;
use Mremi\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 Mremi\UrlShortener\Model\LinkManager;

$linkManager = new LinkManager($chainProvider);

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

$expanded = $linkManager->findOneByProviderAndLongUrl('google', 'http://www.google.com');