Bitly is a great tool for creating short links, in this post we will use their API to generate links dynamically using PHP.
First, you need a token to get one head over to https://app.bitly.com/settings/api/ enter your password to generate an access token. Make a note of the access token.
Create a class called link.php this will be how we're going to create links.
Enter your access token.
This class will hold the token and the link once generated.
class link
{
private string $token = 'your-token-here';
public string $link = '';
Create a construct method and pass in $url, $campaignName, $source and $medium
You may want to make these optional, but lets assume we always want use UTM tags on our links for better tracking.
The link will always contain utm_source, utm_medium and utm_campaign
public function __construct(string $url, string $campaignName, string $source, string $medium)
{
$source = urlencode($source);
$medium = urlencode($medium);
$campaignName = urlencode($campaignName);
$url = $url."?utm_source=$source&utm_medium=$medium&utm_campaign=$campaignName";
$this->link = $this->createLink($url);
}
We need a link to return the link from the class
public function getLink()
{
return $this->link;
}
the last method will be to send a request using curl the the api.
Set the $apiurl, then initialse curl, setup the token header and options to be POSTed to Bitly,
Finally return only the short link, you may want to return the full response if you want more then the short link.
private function createLink($url)
{
$apiurl = "https://api-ssl.bitly.com/v4/bitlinks";
$curl = curl_init($apiurl);
curl_setopt($curl, CURLOPT_URL, $apiurl);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = [
"Content-Type: application/json",
"Authorization: Bearer $this->token",
];
$fields = [
"domain" => "bit.ly",
"long_url" => $url
];
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($fields));
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
curl_close($curl);
return json_decode($resp, true)['link'];
}
Putting it all together:
<?php
class link
{
private string $token = 'your-token-here';
public string $link = '';
public function __construct(string $url, string $campaignName, string $source, string $medium)
{
$source = urlencode($source);
$medium = urlencode($medium);
$campaignName = urlencode($campaignName);
$url = $url."?utm_source=$source&utm_medium=$medium&utm_campaign=$campaignName";
$this->link = $this->createLink($url);
}
public function getLink()
{
return $this->link;
}
private function createLink($url)
{
$apiurl = "https://api-ssl.bitly.com/v4/bitlinks";
$curl = curl_init($apiurl);
curl_setopt($curl, CURLOPT_URL, $apiurl);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = [
"Content-Type: application/json",
"Authorization: Bearer $this->token",
];
$fields = [
"domain" => "bit.ly",
"long_url" => $url
];
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($fields));
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
curl_close($curl);
return json_decode($resp, true)['link'];
}
}
Usage
To create a new link create a new instance of the link class, pass in your options then call ->getLink() to get the shortlink.
$url = 'https://example.com/post/some-post';
$campaignName = 'marketing-run';
$source = 'Twitter';
$medium = 'Tweet';
$link = new link($url, $campaignName, $source, $medium);
$shortUrl = $link->getLink();
When you run you will get the shortlink, You can see your links and stats including the utm tags in your bitley dashboard.
You can use this class to automate short links for instance maybe create a shortlink for every new blog post automatically.
Top comments (0)