The Lob.li API version 2
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
README.md
v2api.php

README.md

lob.li Logo Lob.li API

Version 2

The Lob.li API is licensed under the Creative Commons

Attribution-NonCommercial-ShareAlike 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/

Hey guys, I hope to make this project feaable and I would love your help. If you find a bug, please please submit a bug report. It helps out a lot to let me know what's going on. If you have any suggestions or ideas to make it better, feel free to submit a pull request and I'll look into merging it into the master branch.

The API authentication is (for the moment) only for high traffic usage so that I can keep track and make adjustments to the system and network to make everyone happy.

What is it?

  • Lob.li is a link shortener
  • The API is a tool to let programmers integrate lob.li functionality into their programs
  • It's free to use and will remain that way forever
  • Easy to use
  • Custom link expiry
  • Link resolving - make a short link long again
  • Link stat lookup - Returns the stats that are stored on that link, or as a whole for broader stats

How do I use it?

That's actually pretty easy! I've designed this API to handle a bunch of different inputs to help programmers.

  • Support for GET requests at the endpoint api.lob.li/v2/
  • Support for POST requests at the endpoint api.lob.li/v2/
  • Authenticated and non-authenticated requests of both types
  • SSL/TLS is an available option

The commands are simple to use and grasp (as shown below)

Primary commands (Required):

  • shorten - Used to shorten links
  • resolve - Used to lengthen short links
  • stats - Used to retrive various stored statistics gathered about links

Secondary commands:

  • url - The link in question (Required)
    • shorten - This will be your long url that you want to shorten
    • resolve - This will be one of (shortID, lob.li/shortID, long url)
      • shortID and lob.li/shortID are treated the same
      • long url will act like you're shortening a long url
  • stats - This will display statistics that are gathered during normal link usage, used with modifiers can show specific usage details, or just general click stats/number of users
  • expire - Used only with shorten. Sets expiration time to +secs from NOW();
  • mods - JSON array containing what you want to grab from the stats engine
  • key - Your API key (if you have one, used for high volume/traffic services)

Example usage:

PHP using file_get_contents(); - Unauthenticated GET

<?php 
    $longUrl = "Your website's long url";
    // The API query
    $api = "http://api.lob.li/v2/?shorten&url=$longUrl";
    // Array containing result of the shorten
    $result = json_decode(file_get_contents($api), true);
    
    // Result will be:
    Array('shorten':Array('id'=>"123", 'link'=>"longUrl", 
'datecreated'=>"Today's Date", 'expireat'=>"UNIX timestamp in future 
when link expires - 24h by default"))
?>

PHP using cURL - Unauthenticated POST

<?php
    $longUrl = Your website's long url;
    $url = "http://api.lob.li/v2";
    
    $fields = array('type'=>'shorten', 'url'=>$longUrl);
    foreach($fields as $key=>$value) { $fields_string .= 
$key.'='.$value.'&'; }
    rtrim($fields_string, '&');
    
    //open connection
    $ch = curl_init();
    
    curl_setopt($ch,CURLOPT_URL, $url);
    curl_setopt($ch,CURLOPT_POST, count($fields));
    curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

    //execute post
    $result = json_decode(curl_exec($ch), true);
    
    //close connection
    curl_close($ch);
    
    
    // Result will be:
    Array('shorten':Array('id'=>"123", 'link'=>"longUrl", 
'datecreated'=>"Today's Date", 'expireat'=>"UNIX timestamp in future 
when link expires - 24h by default"))
?>

JavaScript Unauthenticated GET

function sendAPIRequest(url, callback){ 
    var method = "GET";
	var req = new XMLHttpRequest();
	req.open(method, "http://api.lob.li/v2/" + url, true);
	req.onload = function(){
		callback(req);
	};
	req.send();
}

function shortenURL(url){ // Creates a short url and copies it to 
clipboard
    if(testURL(url)){
		var url = encodeURIComponent(url);
		sendAPIRequest("?shorten&url=" + url, function(req){
			var res = req.responseText.trim();
            return res;
		});
	}
}

var longUrl = your long url;
var short = shortenURL(longUrl);

// var short will be a JSON string looking similar to:
{"shorten":{"id":"123","link":"http:\/\/your long 
url","datecreated":"12\/10\/2014","expireat":1418249265}}

// testURL() checks if the link markup is valid and returns true/false