URL shortening has become an essential technique in the digital landscape, enabling users to condense long and complex web addresses into shorter, more manageable links. It not only improves user experience but also enhances shareability and conserves character space.

URL shortening services play a crucial role in today’s digital era, allowing users to create concise and memorable links. Bitly, one of the leading providers in this domain offers a robust API that seamlessly integrates URL-shortening capabilities into applications. With just a few lines of PHP code, you can harness the Bitly API to shorten URLs effortlessly.

In this tutorial, we will guide you through the process of shortening URLs using the Bitly API with a simple but practical PHP example. Here, we will take an example to take URL user input and show shortened links using Bitly APIs.

Before starting sign up or sign in to your Bitly account and obtain the API token. You will need that API token while calling API further.

To begin, let’s create an HTML form that enables users to input a long URL and obtain a shortened version using the Bitly API:

<!DOCTYPE html>
<html>
<head>
    <title>URL Shortener In PHP</title>
</head>
<body>
    <h1>URL Shortener In PHP</h1>
    <form method="POST" action="shorten.php">
        <label for="url">Long URL:</label>
        <input type="text" id="url" name="url" required>
        <button type="submit">Shorten URL</button>
    </form>
</body>
</html>

Here, we have taken simple form and text input for the URL. Next, we will create a PHP script that takes the long URL as input and returns a short one.

Create a new PHP file named shorten.php that handles the form submission and interacts with the Bitly API:

<?php

if (isset($_POST['url'])) {
    $accessToken = 'YOUR_BITLY_ACCESS_TOKEN';
    $url = $_POST['url'];

    $apiEndpoint = 'https://api-ssl.bitly.com/v4/shorten';
    $headers = [
        'Content-Type: application/json',
        'Authorization: Bearer ' . $accessToken,
    ];
    $requestData = [
        'long_url' => $url,
    ];

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $apiEndpoint);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($requestData));

    $response = curl_exec($ch);

    if ($response === false) {
        die('Error: ' . curl_error($ch));
    }

    curl_close($ch);

    $responseData = json_decode($response, true);
    $shortUrl = isset($responseData['id']) ? $responseData['id'] : null;

    if ($shortUrl) {
        echo '<h2>Shortened URL:</h2>';
        echo '<a href="' . $shortUrl . '">' . $shortUrl . '</a>';
    } else {
        echo 'URL shortening failed.';
    }
}

?>

While using this example, replace ‘YOUR_BITLY_ACCESS_TOKEN‘ with your actual Bitly access token.

In the above example, first of all, it will get the URL from the request and define the token for API.

Then, it will perform an API request to Bitly to shorting the URL. With that API call, we will pass an access token to authenticate our request and URL to be shortened.

If everything goes well at Bitly, it will print a shortened URL as output otherwise it will show error details to the user. In most cases, authentication-related errors occur while shortening URLs so double-check your access token before executing code.

Conclusion

In the above example, we have taken a simple example to perform URL shortening using PHP and Bitly as API service providers.

There are many other URL shortener service providers available like tinyURl or Ow.ly. If you have any questions or feedback, please feel free to contact us.