How to use the API

21.Nov.2019

API Usage in PHP

An API key is required for requests to be processed by the system. After a user registers, an API key is automatically generated for that 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 expires, there will be an error. Keep the API key a secret to prevent abuse.

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

GET https://bitli.li/api/?key=YourAPI&url=THELONGURLTOBESHORTENED&custom=CUSTOMALIAS

Server Response

 

As before, the response will be encoded in JSON format (default). This is done for ease of use in multiple languages. The first element of the response always tells you whether an error (error: 1) or not (error: 0) occurred. The second element will change relative to the first element. If there is an error, the second element will be named 'msg'. which contains the source of the error, otherwise it will be called "short" which contains the short URL. (See below for an example)

No mistakes


{ "error": 0, "short": "https: \ / \ / bitli.li \ / DkZOb" }

an error has occurred

 


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

Using Plain Text Format

You can now request that the answer be in plain text by adding format & format = text at the end of your request. Note that if an error occurs, it will produce nothing; so you can assume that if it is empty there will be an error.



Using the API in PHP

To use the API in your PHP application, you must submit a GET request via 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://bitli.li/api/?key=5sIVLhTEMTl0 "; $ 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"];} } ?>

Simple use


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

Use with custom alias


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

Use with custom alias and text format


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

Nous sommes sociaux

Top Blog Posts