RBA Cash Rate: 4.35% · 1AUD = 0.67 USD · Inflation: 4.1%  
Leading Digital Marketing Experts | 1300 235 433 | Aggregation Enquires Welcome | Book Appointment
Example Interest Rates: Home Loan Variable: 5.20% (5.24%*) • Home Loan Fixed: 5.48% (6.24%*) • Fixed: 5.48% (6.24%*) • Variable: 5.20% (5.24%*) • Investment IO: 5.78% (6.81%*) • Investment PI: 5.49% (6.32%*)

Create a Truncated URL with Shor.tt or Fat.ly

Create a Truncated URL with Shor.tt or Fat.ly

We've talked about the need for branded short URL services in the past. However, using third-party tools to represent your business isn't just about branding, it's about preserving the integrity of your links and maintaining control over your data. There no reason why any business – large or small – shouldn't invest in their own themed shortening service...and this is something we address with our partners during early consultations.

Shor.tt Version 2: 20 January 2023. The Shor.tt Module has undergone significant updates. Review the Shor.tt FAQ for ongoing changes.

If the decision for branded truncation isn't made, we make a number of domains available - primarily fat.ly and shor.tt. Because the URLs created by our own shorteners are only shared by our clients, all of which we heavily scrutinize ourselves, there's a quality assurance in knowing that you're not sharing your links with dodgy marketers and practitioner of questionable content.

We've played with different ways of working with caching URL results in the past and we were never particularly satisfied with the results. When using WordPress it's usually necessary to store the results in the WP database. Outside of WP, however, it's important to cache the results to ensure repeated requests aren't made to the API for the same URL. As such, Simple Cache is required for the following functions.

The functions below will be reference a number of times in the future... most notably for Twitter related shortcodes that follow. The functions may be used with our own truncation tools or for the service we've established for your own business.

Create a Truncated URL

The first function is responsible for shortening a URL and returning a truncated link. It returns an array of the URL id, short URL, and original URL.

1
<?php 
2
/*
3
 Create a Truncated URL with Shor.tt or Fat.ly
4
 Returns array, ID and short URL.
5
*/
6
 
7
function beliefmedia_shortt_url($url, $args = '') {
8
 
9
  $atts = array(
10
    'service' => 'shor.tt',
11
    'apikey' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
12
    'alias' => false,
13
    'pw' => false,
14
    'expiry' => false,
15
  );
16
 
17
  /* Merge args */
18
  $atts = ($args == '') ? $atts : array_merge($atts, $args);
19
 
20
  /* Permitted services */
21
  $services = array('shor.tt', 'fat.ly');
22
 
23
  /* Check for basic errors */
24
  if ( ($atts['apikey'] == '') || (!in_array($atts['service'], $services)) ) return 'Invalid Request';
25
 
26
  /* Check for cached result */
27
  $transient = 'beliefmedia_shortturl_' . md5(serialize($atts) . $url);
28
  $cached =  beliefmedia_get_transient_data($transient);
29
 
30
  if ($cached !== false ) {
31
    return $cached['url'];
32
 
33
    } else {
34
 
35
    /* Retrieve Shortt URL */
36
    $encurl = base64_encode($url);
37
 
38
    /* Create Request String */
39
    $request = 'http://' . $atts['service'] . '/api/v1/' . $atts['apikey'] . '/shorturl/create/';
40
 
41
    /* Options */
42
    if ($atts['alias'] !== false) $request .= 'custom_url/' . $atts['alias'] . '/';
43
    if ($atts['pw'] !== false) $request .= 'password/' . $atts['pw'] . '/';
44
    if ($atts['expiry'] !== false) $request .= 'expiry_date/' . $atts['expiry'] . '/';
45
    $request .= 'url/' . $encurl . '.json';
46
 
47
    /* Request */
48
    $json = @file_get_contents($request);
49
    if ($json !== false) $data = json_decode($json, true);
50
    else return $url;
51
 
52
    /* Require code 200 */
53
    $code = $data['code'];
54
    if ($code != '200') return $url;
55
 
56
    /* Get truncated URL */
57
    $result['id'] = $data['data']['url_id'];
58
    $result['url'] = $data['data']['short_url'];
59
    $result['long'] = $data['data']['original_url'];
60
 
61
   /* Cache Response */
62
   beliefmedia_set_transient($transient, $result);
63
 
64
   return $result['url'];
65
  }
66
}

While the id, url, and long url are cached for future retrieval, it's the truncated URL that is returned. The alias and password, if required, should be alphanumeric characters. The expiry should be in the format yyyy-mm-dd.

1
/* Usage */
2
$url = 'http://www.beliefmedia.com';
3
echo beliefmedia_shortt_url($url);

Alter Status of URL

To enable and disable your truncated link, use the following. A Boolean value is returned.

1
<?php 
2
/*
3
 Create a Truncated URL with Shor.tt or Fat.ly
4
 Alters status of URL. Returns Boolean.
5
*/
6
 
7
function beliefmedia_shortt_status($url, $status = '0', $args = '') {
8
 
9
  $atts = array(
10
    'service' => 'shor.tt',
11
    'apikey' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
12
  );
13
 
14
  /* Action */
15
  $status = ($status == '0') ? 'disable' : 'enable';
16
 
17
  /* Merge args */
18
  $atts = ($args == '') ? $atts : array_merge($atts, $args);
19
 
20
  /* Retrieve Shortt URL */
21
  $encurl = base64_encode($url);
22
 
23
  /* Create Request String */
24
  $request = 'http://' . $atts['service'] . '/api/v1/' . $atts['apikey'] . '/shorturl/' . $status . '/short_url/' . $encurl . '.json';
25
 
26
  /* Get contents */
27
  $json = @file_get_contents($request);
28
  if ($json !== false) $data = json_decode($json, true);
29
  else return false;
30
 
31
  /* Check on JSON status */
32
  return ($data['code'] == '200') ? true : false;
33
}
34
 
35
To disable a link, use <code>$status = '0'</code>. To enable, use <code>$status = '1'</code>.
36
<pre>$url = 'http://shor.tt/a214';
37
echo (beliefmedia_shortt_status($url, $status = '0') !== false) ? 'Done' : 'Error';

Return Link Information

To return basic information on a truncated URL, use the following:

1
<?php 
2
/*
3
 Create a Truncated URL with Shor.tt or Fat.ly
4
 Returns truncated URL information.
5
*/
6
 
7
function beliefmedia_shortt_info($url, $args = '') {
8
 
9
  $atts = array(
10
    'service' => 'shor.tt',
11
    'apikey' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
12
    'cache' => '43200'
13
  );
14
 
15
  /* Merge args */
16
  $atts = ($args == '') ? $atts : array_merge($atts, $args);
17
 
18
  /* Permitted services */
19
  $services = array('shor.tt', 'fat.ly');
20
 
21
  /* Check for basic errors */
22
  if ( ($atts['apikey'] == '') || (!in_array($atts['service'], $services)) ) return false;
23
 
24
  /* Check for cached result */
25
  $transient = 'beliefmedia_shortturl_info_' . md5(serialize($atts) . $url);
26
  $cached =  beliefmedia_get_transient($transient, $atts['cache']);
27
 
28
  if ($cached !== false ) {
29
    return (array) $cached;
30
 
31
    } else {
32
 
33
    /* Retrieve Shortt URL */
34
    $encurl = base64_encode($url);
35
 
36
    /* Create Request String */
37
    $request = 'http://' . $atts['service'] . '/api/v1/' . $atts['apikey'] . '/shorturl/info/short_url/' . $encurl . '.json';
38
 
39
    /* Request */
40
    $json = @file_get_contents($request);
41
    if ($json !== false) $data = json_decode($json, true);
42
    else return false;
43
 
44
    /* Require code 200 */
45
    $code = $data['code'];
46
    if ($code != '200') return false;
47
 
48
    /* Get truncated URL */
49
    $result['id'] = $data['data']['url_id'];
50
    $result['url'] = $data['data']['short_url'];
51
    $result['long'] = $data['data']['original_url'];
52
    $result['created'] = $data['data']['date_created'];
53
    $result['accessed'] = $data['data']['last_accessed'];
54
    $result['visits'] = $data['data']['total_visits'];
55
    $result['expiry'] = $data['data']['expiry_date'];
56
    $result['status'] = $data['data']['status'];
57
    $result['time'] = $data['data']['date_time'];
58
 
59
   /* Cache Response */
60
   beliefmedia_set_transient($transient, $result);
61
 
62
   return $result;
63
  }
64
}

The function returns an array of URL data. Returns false if invalid.

Get URL Count

To return the count of an existing short URL, the following example function may be used.

1
<?php 
2
/*
3
 Create a Truncated URL with Shor.tt or Fat.ly
4
 Returns truncated URL count.
5
*/
6
 
7
function beliefmedia_shortt_count($url, $args = '') {
8
 
9
  $atts = array(
10
    'service' => 'shor.tt',
11
    'apikey' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
12
    'cache' => '43200'
13
  );
14
 
15
  /* Merge args */
16
  $atts = ($args == '') ? $atts : array_merge($atts, $args);
17
 
18
  /* Check for cached result */
19
  $transient = 'beliefmedia_shortturl_info_' . md5(serialize($atts) . $url);
20
  $cached =  beliefmedia_get_transient($transient, $atts['cache']);
21
 
22
  if ($cached !== false) {
23
    return $cached['visits'];
24
 
25
    } else {
26
 
27
   $results = beliefmedia_shortt_info($url, $atts);
28
   return $results['visits'];
29
 }
30
}

Considerations

  • Caching is prone to errors if data, or the data $atts array, isn't consistent. All the functions described on this page are built into a client plugin that hosts an independent version of caching, and includes a control panel to set API Keys and other default values.
  • For those that have access to the API features of one of our shorteners but aren't connected to our social system, the Simple Cache functions can be download and installed as a plugin.

Download our 650-page guide on Finance Marketing. We'll show you exactly how we generate Billions in volume for our clients.

  E. Australia Standard Time [ UTC+10, Default ] [ CHECK TO CHANGE ]

  Want to have a chat?
 

RELATED READING

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest

Leave a comment