Create TinyURL Using Bitly API And PHP

Recently, we have published our article Create TinyURL Using Google URL Shortener API. One of our readers asked to write also the article on creating TinyURL using the Bitly. So in this post, we study how to create a short link using the Bitly API and PHP.

We all know, creating short links makes it easy to share our URLs on social media, emails and with our clients. That’s why services like Bitly, Google Shortener, etc became popular on the web.

Bitly is a URL shortener and link management platform. They allow us to create short links and track the individual link analytics.

Bitly provides API limitation on the basis of per-month, per-hour, per-minute. You can read about their API limitation here.

If you are running a website with few hundreds of links then free quota provided by Bitly will be sufficient for you.

Get Started

To get started, at first you need to have account on Bitly.

As we are going to use Bitly API, we need to generate access token which is required to interact with the APIs. To generate the access token, go to your Edit Profile page.

Click on the top-right settings menu and then select your username/email.

Your Account

On the next page, you will see the option for ‘Generic Access Token’. Click on it and copy your access token which we need in a moment.

Access Token

Use Guzzle For API Calls

For creating a TinyURL, we have to send HTTP request to the Bitly API endpoints.

We can use either Guzzle or CURL for sending HTTP request. But, we recommend to use Guzzle as it does not require CURL extension enabled on your server.

Having said that, let’s first install Guzzle library in your project. Open the terminal in your project root directory and run the command composer require guzzlehttp/guzzle.

Now, we are ready with the access token and Guzzle library. Create a file tinyurl.php and add the below code to it.


require_once "vendor/autoload.php";

use GuzzleHttp\Client;

$client = new Client([
    // Base URI is used with relative requests
    'base_uri' => '',

$response = $client->request('GET', 'v3/shorten', [
    'query' => [
        'longUrl' => 'PASTE_LONG_URL_HERE',
        'access_token' => 'YOUR_ACCESS_TOKEN',
    'verify' => false,

if($response->getStatusCode() == 200) {
    $body = $response->getBody();
    $arr_body = json_decode($body);
    echo $arr_body->data->url;

Make sure to replace the placeholders in the above code. This code gives a call to the with the endpoint v3/shorten. In response, we get our TinyURL.

Use CURL For API Call

We are recommending Guzzle for giving API call but still, if someone wants to use CURL then below is the code they can use. Actually, a choice is up to the user. If you are using CURL already on your application then obviously you go for CURL. Both methods are giving the same result. So decide any one method which is suitable for you.

$url = '';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

$arr_result = curl_exec($ch);

$arr_response = json_decode($arr_result);

echo $arr_response->data->url;

If you liked this article, then please subscribe to our Youtube Channel for video tutorials.

Recommended Tutorials For You

Posted in PHP

Leave a Reply

Your email address will not be published. Required fields are marked *