Are you looking to create short links for long URLs? Short link or TinyURL is easy to share via email and on social media. When a user clicks the short URLs, they’re automatically forwarded to the destination URL.
Bitly is a popular URL shortener service. They allow us to create short links and track the individual link analytics. In this article, we study how to create a short link using the Bitly API and PHP.
When we generate TinyURLs with Bitly, your link will be something like bit.ly/3PCDJW2
where bit.ly
is a domain and 3PCDJW2
is a random string assigned for a long URL. If you run or hit this short URL, it first goes to the bit.ly domain and then will redirect automatically to the main URL.
Bitly uses HTTP 301 redirects for its links. The 301 status code means permanent redirecting.
Getting Started
To get started, you first need to have an account on Bitly.
In order to use Bitly API, you have to generate an access token. The access token is mandatory to interact with the APIs. During the API call, you must send this token in the Authorization header.
To generate the access token, click on Settings from under the top-right corner of your username.
On the next page, click on Developer Settings => API. Enter your password and you’ll get the access token. Copy this token as we need it in a moment.
Next, to create a short link with Bitly, it needs to send the HTTP POST request to the API endpoint. In PHP, you can send HTTP requests with Guzzle and cURL. I am going to write a code for both Guzzle and cURL so users can pick the one fitting their application.
Send HTTP Request Using Guzzle
For sending HTTP requests, I personally recommend using Guzzle as it does not require a cURL extension enabled on your server. Additionally, Guzzle provides a much cleaner code as compared to cURL.
Install the Guzzle library in your project using the command:
composer require guzzlehttp/guzzle
Upon package installation, write the below code in your PHP file which generates TinyURL for you.
<?php
require_once "vendor/autoload.php";
use GuzzleHttp\Client;
$token = "ACCESS_TOKEN";
$long_url = "LONG_URL";
try {
$client = new Client([
// Base URI is used with relative requests
"base_uri" => "https://api-ssl.bitly.com",
]);
$response = $client->request("POST", "/v4/bitlinks", [
"json" => [
"long_url" => $long_url,
],
"headers" => [
"Authorization" => "Bearer $token"
]
]);
if(in_array($response->getStatusCode(), [200, 201])) {
$body = $response->getBody();
$arr_body = json_decode($body);
echo $arr_body->link;
}
} catch(Exception $e) {
echo $e->getMessage();
}
Make sure to replace the placeholders ACCESS_TOKEN and LONG_URL with their actual values. This code sends a POST request to the https://api-ssl.bitly.com/v4/bitlinks
along with a long URL. In response, you will get your short URL. Try to visit this short URL and eventually, you’ll be forwarded to the original URL.
Send HTTP Request Using cURL
<?php
$api_url = "https://api-ssl.bitly.com/v4/bitlinks";
$token = "ACCESS_TOKEN";
$long_url = "LONG_URL";
$ch = curl_init($api_url);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(["long_url" => $long_url]));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: Bearer $token",
"Content-Type: application/json"
]);
$arr_result = json_decode(curl_exec($ch));
echo $arr_result->link;
All short links generated via Bitly API are available inside the Bitly dashboard. On the dashboard, you will also get insights into these links that can be used for marketing purposes.
I hope you understand how to create TinyURL using Bitly API in PHP. I would like to hear your thoughts and suggestions in the comment section below.
Related Articles
- Speech-To-Text using Amazon Transcribe in PHP
- How to Integrate Google Sheets API with PHP
- Text-To-Speech using Amazon Polly in PHP
If you liked this article, then please subscribe to our YouTube Channel for video tutorials.
How can i get clicks and countries of a url in bitly please make an article for it
this is perfect however v3 will be depracated i wonder if you can remake this script to work on the upcoming v4?
Thanks for the updates. Will go through it and update the article soon.
I updated article which is now working with the latest Bitly API version V4.