Developer

If you want to use our API, you may do so by simply following these instructions. To prevent abuse, we require our developers to register an account and make calls using their assigned key. If you have not yet registered, simply click here. In the other hand if you already have an account, simply login and retrieve your developer key.

URL REQUEST TO SHORTEN A LONG URL

Request are done by sending the the long url to http://btfd.ca/api/. The URL will then be processed by our server and the a short url will be generated. Below is an example of a sample request. Note that request are only accept if and only you also send your API keywhich is assigned when you register to our site.

GET http://btfd.ca/api/?url=http://www.google.com&api=APIKEY

Below is a sample response. Note that response are returned in json format. This means that you will need the decode that use the native decoding of the language you use in your application. If there are no errors encountered during the process, the "error" element will be set to 0, otherwise to 1. You can use this to validate the content before using it.

SAMPLE RESPONSE

{
"error":"0",

"short":http://btfd.ca/mnQRi"
}

SAMPLE USAGE IN PHP

$content=@file_get_contents("http://btfd.ca/api/?url=http://www.google.com&api=APIKEY");

$url=json_decode($content,TRUE);//Decodes json into an array

if(!$url["error"]){ // If there is no error
echo $url["short"]; //Outputs the short url
}else{
echo $url["msg"]; //Outputs the error message
}

HANDLING ERRORS

If there is a problem, the error element will be set to "0" and an error message will be set. There are several error messages. For example, if the API key is not set or is invalid, an error message will tell you that.

These are some cases you can expect an error:

  • Invalid URL or is simple not set
  • API Key is invalid or is not set
  • The administrator has disabled the API system.

SAMPLE ERRORS RESPONSE

{

"error":"1",

"msg":"Service have been disabled."
}

Download the Wordpress Plugin

Wordpress Plug-in Setting up the function

First we will start setting up a function to send the request via the API and to display the result. That function must be added to the functions.php  file of your theme. If you are using a decent WordPress theme, chances are that that file already exists. If not you can simply create one in your theme folder.

Once that file has been created, you must paste the code below in it and add your API key .

function shorten_url($url){

    $siteurl="http://btfd.ca"; 

    $apikey="YOUR API KEY"; // You can get it from the user account

    if($apikey && $siteurl){

        $short=@file_get_contents("$siteurl/api?api=$apikey&url=".strip_tags(trim($url)));

        $short=json_decode($url,TRUE);

        if(!$short["error"]){

            return $short["short"];

        }

    }

}

Make sure to replace $apikey by the api key you get from the user panel. An important thing to note about this function is that it does not show the error message. It will only display the short url when it has been successfully shortened. Another thing to note is the name of the function. If you think this name has been taken by a plugin or something else  (which may happen if you use a lot of plugins), then consider changing it to something unique e.g. make_this_url_short($url). Be creative.

ADD A SHORTCODE FEATURE

If you are an experienced WordPress user then chances are that you became addicted to shortcodes. These are built in to simplify and to speed writing of content. Registering a shortcode in WordPress is very easy to do. We will use the function above in combination with another simple function to register the shortcode and shorten the URL via the shortcode.

We will again add the following codes in functions.php  of the WordPress theme.

/* This function parses the shortcode content and send it to the main unction */

function shortcode_shorten_url($atts,$content){

   return shorten_url($content);

}

// This code simply registers the shortcode "shorten"

add_shortcode("shorten", "shortcode_shorten_url");

The code above registers the shortcode [ shorten ]THE URL[ /shorten ]. Below is the full code you must add in your theme functions.php.

// Main Function

function shorten_url($url){

    $siteurl="http://btfd.ca";

    $apikey="YOUR API KEY"; // You can get it from the user account

    if($apikey && $siteurl){

        $short=@file_get_contents("$siteurl/api?api=$apikey&url=".strip_tags(trim($url)));

        $short=json_decode($short,TRUE);

        if(!$short["error"]){

            return $short["short"];

        }

    }

}

// Shortcode function

function shortcode_shorten_url($atts,$content){

   return shorten_url($content);

}

// Register Shortcode

add_shortcode("shorten", "shortcode_shorten_url");

To shorten a URL in your post, simply use [ shorten ]THEURL [ /shorten ]. To shorten things like the permalink of the post or page, see the example below.

SHORTENING PERMALINKS IN YOUR THEME FILES

Now that you set up the function, you can freely use it anywhere you want. All you have to do is to call the main function and provide the URL. If for example you want to shorten the permalink of a post or a page you can use the following code in any part of your template.

echo shorten_url(get_permalink()) ?>

Nous sommes sociale

Facebook Likes

Aimez-nous sur FacebookSuivez-nous surTwitter