Developers

Developers API Usage in PHP

An API key is required for requests to be processed by the system. Once a user registers, an API key is automatically generated for this user. The API key must be sent with each request via the key parameter (see full example below). If the API key is not sent or is expired, there will be an error. Please make sure to keep your API key secret to prevent abuse.

Your API key

rfNuGOCZ3Q1X

Sending a request for shortening a URL

To send a request, the user must use the following format where the variables api and url are required. In the example below, the URL of the demo is used but you should use your own domain name. To request a custom alias, simply add &custom= at the end.

GET https://bitcaly.com/api/?key=rfNuGOCZ3Q1X&url=THELONGURLTOBESHORTENED&custom=CUSTOMALIAS

For premium members only

GET https://bitcaly.com/api/?key=rfNuGOCZ3Q1X&url=THELONGURLTOBESHORTENED&custom=CUSTOMALIAS&type=REDIRECTYPE

 

Server response

As before, the response will encoded in JSON format (default). This is done to facilitate cross-language usage. The first element of the response will always tell if an error has occurred (error: 1) or not (error: 0). The second element will change with respect to the first element. If there is an error, the second element will be named 'msg'. which contains the source of error, otherwise it will be named “short” which contains the short URL. (See below for an example)

No errors

{"error":0,"short":"https:\/\/bitcaly.com\/DkZOb"}

 

An error has occurred

{"error":1,"msg":"Please enter a valid url" }

Using plain text format

You can now request the response to be in plain text by just adding &format=text at the end of your request. Note that if an error occurs, it will not output anything so you can assume if it is empty then there is an error.


Using the API in PHP

To use the API in your PHP application, you have to send a GET request through file_get_contents or cURL: Both are reliable methods. You can copy the function below. Everything is already set up for you

/**** Sample PHP Function ***/function shorten($url, $custom = "", $format = "json") { $api_url = "https://bitcaly.com/api/?key=rfNuGOCZ3Q1X";$api_url .= "&url=".urlencode(filter_var($url, FILTER_SANITIZE_URL));if(!empty($custom)){$api_url .= "&custom=".strip_tags($custom);}$curl = curl_init();curl_setopt_array($curl, array(CURLOPT_RETURNTRANSFER => 1,CURLOPT_URL => $api_url));$Response = curl_exec($curl);curl_close($curl);if($format == "text"){$Ar = json_decode($Response,TRUE);if($Ar["error"]){return $Ar["msg"];}else{return $Ar["short"];}}else{return $Response;}}?>

 

Simple Usage

echo shorten("https://google.com");?>

Usage with custom alias

echo shorten("https://google.com", "google");?>

Usage with custom alias and text format

echo shorten("https://google.com", "google", "text");?>

 

Full-Page Script

This script allows you to shorten all (or select) URLs on your website very easily. All you need to do is to copy and paste the code below at the end of your page. You can customize the selector as you wish to target URLs in a specific selector. Note you can just copy the code below because everything is already for you.

 
Choosing custom select

By default, this script shortens all URLs in a page. If you want to target specific URLs then you can add a selector paramater. You can see an example below where the script will only shorten URLs having a class named mylink or all direct link in the .content container or all links in the .comments container

 
Excluding domain names

You can exclude domain names if you wish. You can add an exclude parameter to exclude domain names. The example below shortens all URLs but excludes URLs from google.com or bitcaly.com