Code Monkey home page Code Monkey logo

urlshortener's Introduction

URL shortener library

This library allows you to shorten a URL, reverse is also possible.

SensioLabsInsight

Build Status Total Downloads Latest Stable Version Scrutinizer Quality Score Code Coverage

Basic Docs

Installation

Only 1 step:

Download UrlShortener using composer

Add UrlShortener in your composer.json:

{
    "require": {
        "mremi/url-shortener": "dev-master"
    }
}

Now tell composer to download the library by running the command:

$ php composer.phar update mremi/url-shortener

Composer will install the library to your project's vendor/mremi directory.

Bit.ly API

Shorten

<?php

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 OAuthClient('username', 'password'), // or new GenericAccessTokenAuthenticator('generic_access_token')
    array('connect_timeout' => 1, 'timeout' => 1)
);

$bitlyProvider->shorten($link);

You can also use the commands provided by this library, look at the help message:

$ bin/shortener bitly:shorten --help

Some arguments are mandatory:

$ bin/shortener bitly:shorten username password http://www.google.com

Some options are available:

$ bin/shortener bitly:shorten username password http://www.google.com --options='{"connect_timeout":1,"timeout":1}'

Expand

<?php

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 OAuthClient('username', 'password'), // or new GenericAccessTokenAuthenticator('generic_access_token')
    array('connect_timeout' => 1, 'timeout' => 1)
);

$bitlyProvider->expand($link);
$ bin/shortener bitly:expand --help

Some arguments are mandatory:

$ bin/shortener bitly:expand username password http://bit.ly/ze6poY

Some options are available:

$ bin/shortener bitly:expand username password http://bit.ly/ze6poY --options='{"connect_timeout":1,"timeout":1}'

Google API

Shorten

<?php

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',
    array('connect_timeout' => 1, 'timeout' => 1)
);

$googleProvider->shorten($link);

You can also use the commands provided by this library, look at the help message:

$ bin/shortener google:shorten --help

Only one argument is mandatory (the long URL) but you can also provide a Google API key:

$ bin/shortener google:shorten http://www.google.com
$ bin/shortener google:shorten http://www.google.com api_key

Some options are available:

$ bin/shortener google:shorten http://www.google.com --options='{"connect_timeout":1,"timeout":1}'

Expand

<?php

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',
    array('connect_timeout' => 1, 'timeout' => 1)
);

$googleProvider->expand($link);
$ bin/shortener google:expand --help

Only one argument is mandatory (the short URL) but you can also provide a Google API key:

$ bin/shortener google:expand http://goo.gl/fbsS
$ bin/shortener google:expand http://goo.gl/fbsS api_key

Some options are available:

$ bin/shortener google:expand http://goo.gl/fbsS --options='{"connect_timeout":1,"timeout":1}'

Chain providers

<?php

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

Retrieve link

You can retrieve some links using these finders:

<?php

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

Contribution

Any question or feedback? Open an issue and I will try to reply quickly.

A feature is missing here? Feel free to create a pull request to solve it!

I hope this has been useful and has helped you. If so, share it and recommend it! :)

@mremitsme

urlshortener's People

Contributors

mremi avatar studiomax avatar drmonkeyninja avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.