phpbit.ly

How to shorten URL using PHP Bitly v4?


I have this code for Bitly v3 and it is working well.

<?php
$login = 'login-code-here';
$api_key = 'api-key-here';
$long_url = 'https://stackoverflow.com/questions/ask';

$ch = curl_init('http://api.bitly.com/v3/shorten?login='.$login.'&apiKey='.$api_key.'&longUrl='.$long_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$res = json_decode($result, true);
echo $res['data']['url']; // bit.ly/2PcG3Fg
?>

However, how can this be done in the newer version? The above example uses API keys but it has been deprecated in favor of OAuth requests.

How to shorten URL using Bitly v4?


Solution

  • Get generic access token

    Go to your Bitly, click on the hamburger menu on the top-right side > Settings > Advanced Settings > API Support > click on the link Generic Access Tokens. Type in your password and generate a generic token. That's what you'll use for authentication.

    See https://dev.bitly.com/v4_documentation.html and look for Application using a single account section.

    Authentication has changed a bit according to https://dev.bitly.com/v4/#section/Application-using-a-single-account.

    How you authenticate to the Bitly API has changed with V4. Previously your authentication token would be provided as the access_token query parameter on each request. V4 instead requires that the token be provided as part of the Authorization header on each request.

    Code

    See this doc https://dev.bitly.com/v4/#operation/createFullBitlink for information about what Bitly expects.

    In v4, you could use generic token as a bearer in your headers with each request like so:

    <?php
    
    $long_url = 'https://stackoverflow.com/questions/ask';
    $apiv4 = 'https://api-ssl.bitly.com/v4/bitlinks';
    $genericAccessToken = 'your-token';
    
    $data = array(
        'long_url' => $long_url
    );
    $payload = json_encode($data);
    
    $header = array(
        'Authorization: Bearer ' . $genericAccessToken,
        'Content-Type: application/json',
        'Content-Length: ' . strlen($payload)
    );
    
    $ch = curl_init($apiv4);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    $result = curl_exec($ch);
    
    print_r($result);
    

    Request

    JSON that you send will look like this:

    {"long_url":"https:\/\/stackoverflow.com\/questions\/ask"}

    Response

    {
       "created_at":"1970-01-01T00:00:00+0000",
       "id":"shortcode-link-id-here",
       "link":"shortcode-link-here",
       "custom_bitlinks":[
    
       ],
       "long_url":"https://stackoverflow.com/questions/ask",
       "archived":false,
       "tags":[
    
       ],
       "deeplinks":[
    
       ],
       "references":{
          "group":"group-link-here"
       }
    }
    

    EDIT

    There is a request in comments to see just the short link output. In order to do that, just adapt the code like so:

    <?php
    $long_url = 'https://stackoverflow.com/questions/ask';
    $apiv4 = 'https://api-ssl.bitly.com/v4/bitlinks';
    $genericAccessToken = 'your-token';
    
    $data = array(
        'long_url' => $long_url
    );
    $payload = json_encode($data);
    
    $header = array(
        'Authorization: Bearer ' . $genericAccessToken,
        'Content-Type: application/json',
        'Content-Length: ' . strlen($payload)
    );
    
    $ch = curl_init($apiv4);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    $result = curl_exec($ch);
    $resultToJson = json_decode($result);
    
    if (isset($resultToJson->link)) {
        echo $resultToJson->link;
    }
    else {
        echo 'Not found';
    }
    

    Result (assuming the above file was test.php)

    php test.php
    
    bit.ly/2ZbYD4Z