How to generate a Bitly short link with PHP

remarkablemark
Mar 14, 2025

--

Bitly logo
Bitly

This article goes over how to generate a Bitly short link with PHP.

Prerequisites

You should be using PHP 8+:

php --version

Generate a Bitly access token.

Composer

Install with Composer:

composer require bitly-api/sdk php-http/guzzle7-adapter

SDK

Create the script:

touch script.php

Instantiate the SDK:

<?php

require_once 'vendor/autoload.php';

$bitly = new Bitly\Bitly('YOUR_API_KEY'); // replace access token

Convert a long URL to a Bitlink:

$shorten = new Bitly\Model\Shorten();
$shorten->setLongUrl('https://example.com/my-long-url'); // replace url
$response = $bitly->client->createBitLink($shorten);

echo $response->link;

See the full script:

<?php

require_once 'vendor/autoload.php';

$bitly = new Bitly\Bitly('YOUR_API_KEY'); // replace access token

$shorten = new Bitly\Model\Shorten();
$shorten->setLongUrl('https://example.com/my-long-url'); // replace url
$response = $bitly->client->createBitLink($shorten);

echo $response->link;

Resources

--

--

No responses yet