Updated bitly.php in fabrik library

SoilentRed

Caaan do!
I finally have something to contribute! Hopefully this helps someone and can be used in J4 (I'm using this on J3).

Bitly went ahead and updated their API to version 4. So my forms stopped shortening my long URLs. With the new version, you no longer use a secret key and account name to connect. Instead, you use an access token. And specify a group ID if available. I have mine set to a default group. Here's what I'm using that works for me:

Code:
<?php
// No direct access
defined('_JEXEC') or die('Restricted access');
class bitly {
    var $status_code = '200';
    var $status_txt = 'OK';
    protected $access_token;
    /**
     * Contruct the Bitly Class
     * @param string, your Bitly access token
     */
    public function __construct($access_token) {
        $this->access_token = $access_token;
    }
    /**
     * Was there an error on the last shorten attempt, return code or 0 for no error
     * @return integer, status code
     */
    public function getError() {
        if ($this->status_code == '200') {
            return 0;
        }
        else {
            return (int) $this->status_code;
        }
    }
    /**
     * Return error status msg from last shorten attempt
     * @return string, status code + txt
     */
    public function getErrorMsg() {
        return $this->status_code . ': ' . $this->status_txt;
    }
    /**
     * Retrieve a shortened URL
     * @param string, url to shorten
     * @param string, group_guid to set the group of the new Bitlink (optional)
     */
    public function shorten($url, $group_guid = null) {
        //create the URL
        $api_url = 'https://api-ssl.bitly.com/v4/shorten';   
        //create headers
        $headers = array(
            'Authorization: Bearer '.$this->access_token,
            'Content-Type: application/json',
        );
        //create request body
        $data = array(
            'long_url' => $url,
        );
        if ($group_guid !== null) {
            $data['group_guid'] = $group_guid;
        }
        $data_json = json_encode($data);
        //send the request
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $api_url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data_json);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        $response = curl_exec($curl);
        curl_close($curl);
        //parse the JSON response and return the url
        $response_obj = json_decode($response);
        if (isset($response_obj->link)) {
            // success, set OK status and return the shortened URL
            $this->status_code = '200';
            $this->status_txt = 'OK';
            return $response_obj->link;
        }
        else {
            // ooops, set status to returned values from bit.ly, and return the original URL
            $this->status_code = $response_obj->status;
            $this->status_txt = $response_obj->description;
            return $url;
        }
    }
}

Then, using the calc element, you can use this to store a tiny url.

Example:
Code:
// this is a shortened example of one of the many uses we have for fabrik + bitly
$fullUrl = 'https://www.sitename.com/index.php/renewal-agreement?afab_renewal_agreement___group_id='.'{afab_send_renewal_agreement___group_raw}'.'&afab_renewal_agreement___applicant_name='.'{afab_send_renewal_agreement___applicant_name}';
         
require_once JPATH_SITE . '/components/com_fabrik/libs/bitly/bitly.php';
$bitly = new bitly('YOUR_ACCESS_TOKEN_HERE');
       
// bitlify it
$url = (string) $bitly->shorten($fullUrl);
       
return $url;

Couldn't have done it without my friend, ChatGPT! Hopefully, this helps someone.

Cheers!
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top