PHP code example of phplicengine / bitly

1. Go to this page and download the library: Download phplicengine/bitly library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

phplicengine / bitly example snippets


use PHPLicengine\Api\Api;
use PHPLicengine\Service\Bitlink;

$api = new Api("API KEY GOES HERE");
$bitlink = new Bitlink($api);
$result = $bitlink->createBitlink(['long_url' => 'http://www.example.com']);

// if cURL error occurs.
if ($api->isCurlError()) {
    
    print($api->getCurlErrno().': '.$api->getCurlError());
    
} else {

    // if Bitly response contains error message.
    if ($result->isError()) {

        print("Error:<br />");
        print($result->getResponse());
        print($result->getDescription());
    
    } else {
    
        // if Bitly response is 200 or 201
        if ($result->isSuccess()) {
        
            print("SUCCESS:<br />");
            print($result->getResponse());
            print_r($result->getResponseArray());

        } else {

            print("FAIL:<br />");
            print($result->getResponse());
            print_r($result->getResponseArray());

        }
    }
}

// for debug only.
print("INFO:<br />");

// returns response exactly as it is. e.g. json.
$resj = $result->getResponse();
print($resj."<br />");

// returns decoded json.
$reso = $result->getResponseObject();
print_r($reso);

// returns header of server.
$resh = $result->getHeaders();
print_r($resh);

// returns request.
$resr = $api->getRequest();
print_r($resr);

use PHPLicengine\Api\Api;
use PHPLicengine\Service\Bitlink;

$api = new Api("API KEY GOES HERE");
$bitlink = new Bitlink($api);
$result = $bitlink->getMetricsForBitlinkByCountries('bit.ly/34nRNvl', ['unit' => 'day', 'units' => -1]);

use PHPLicengine\Api\Api;
use PHPLicengine\Service\Group;

$api = new Api("API KEY GOES HERE");
$group = new Group($api);
$result = $group->getGroupShortenCounts($group_guid);

$api->setTimeout(30);

use PHPLicengine\Api\Api;
use PHPLicengine\Service\Bitlink;

$api = new Api("API KEY GOES HERE");
$api->setCurlCallback(function($ch, $params, $headers, $method) { 
      curl_setopt($ch, CURLOPT_*, 'some value'); 
}); 
$bitlink = new Bitlink($api);