RedJumpsuit

jobberBase custom development and support

 

Code School

Generating bit.ly URL in jobberBase

i was searching for a php class that interacts easily with the bit.ly API and found a really good one here: http://bit.ly/oDwGX

after studying the code, i thought of a way to integrate this with jobberBase and dynamically create a bit.ly URL when jobs are posted using the PHP class that i found.

just follow the instructions below.

1) create a bit.ly account (if you do not have yet) and grab your API key

2) download the bit.ly API interaction class found in the website i mentioned http://bit.ly/oDwGX and save it as ‘bitly.php’ on your /_includes/ folder

3) run this sql script on your ‘jobs’ table (be sure to make a backup of your table first!)

ALTER TABLE `jobs` ADD `bitly` VARCHAR( 100 ) NULL;

4) open up your root’s ‘config.php’ and below this line:

require_once '_includes/smarty/libs/Smarty.class.php';

add this line:

// bit.ly class
require_once '_includes/bitly.php';

5) open up the bitly.php class you saved on /_includes/ folder and look for this function:

public function shorten($url, $returnHash = false)
{
...
}

below it, paste this new code:

/**
Shorten URL for jobberBase
author: RedJumpsuit <myredjumpsuit@gmail.com>
url: www.redjump.co.cc
**/
public function shorten_jobber($id,$url,$returnHash=false)
{
	global $db;
	$sql = 'SELECT bitly
			FROM jobs
			WHERE id = '. $id;
	$result = $db->query($sql);
	$row = $result->fetch_assoc();
	if ($row['bitly'] == '')
	{
		$bitlyurl = 'http://api.bit.ly/shorten?version='.$this->version.'&longUrl='.$url.'&login='.$this->login.'&apiKey='.$this->apikey.'&format='.$this->format;
		if ( $this->getResult($bitlyurl) !== false )
		{
			$sqlu = 'UPDATE jobs SET bitly = "'. $this->results['shortUrl'] .'" WHERE id = '. $id;
			$db->execute($sqlu);
			if ( $returnHash == true ) {
				return array('shortUrl' => $this->results['shortUrl'], 'hash' => $this->results['hash']);
			} else {
				return $this->results['shortUrl'];
			}
		}
		return false;
	}
}

6) open up ‘page_verify.php’ on your root folder and add this:

// generate bit.ly URL for jobberBase
// author: RedJumpsuit <myredjumpsuit@gmail.com>
// url: www.redjump.co.cc
$joburl = BASE_URL . 'job/' . $id . '/';
$bitly = new bitly('redjumpsuit', 'your API key goes here');
$bitly->shorten_jobber($id,$joburl);

before this block:

$smarty->assign('job', $jobs);
$html_title = stripslashes($jobs['title']) . ' at ' . stripslashes($jobs['company']) . ' / ' . SITE_NAME;
$template = 'publish-verify.tpl';

7) to enable this in the admin panel, open up page_edit_post.php on your /admin/ folder and replace this line:

$job->Create($data);

with this:

$id = $job->Create($data);
 
// generate bit.ly URL for jobberBase
// author: RedJumpsuit <myredjumpsuit@gmail.com>
// url: www.redjump.co.cc
$joburl = BASE_URL_ORIG . 'job/' . $id . '/';
$bitly = new bitly('redjumpsuit', 'your API key goes here');
$bitly->shorten_jobber($id,$joburl);

now it’s up to you to grab the bit.ly url from the ‘jobs’ table, should be easy from this point on right?

if you like my post, use the donate button below :)

Tagged as: , ,

2 Comments

Code School

  1. I tried using the bit.ly url instead of the regular one, but can’t get it to work. Any help/ tips would be appreciated :)

  2. Making this work for version 1.9 requires the following changes (please note, I have not refactored

    any code, all changes are merely additions to the existing code.

    The step numbers refer to those listed in the post.

    #3 – If you have changed the name of the ‘jobs’ table
    before: ALTER TABLE `jobs` ADD `bitly` VARCHAR( 100 ) NULL;
    after: ALTER TABLE `[your table name]` ADD `bitly` VARCHAR( 100 ) NULL;

    #4 – Add APP_PATH to this line:
    before: require_once ‘_includes/bitly.php’;
    after: require_once APP_PATH . ‘_includes/bitly.php’;

    #5 – Add DB_PREFIX to these lines:
    before: $sql = ‘SELECT bitly FROM jobs…
    after: $sql = ‘SELECT bitly FROM ‘.DB_PREFIX.’jobs…

    before: $sqlu = ‘UPDATE jobs SET bitly…
    after: $sqlu = ‘UPDATE ‘.DB_PREFIX.’jobs SET bitly…

    #6 – Add URL_JOB to this line:
    before: $joburl = BASE_URL . ‘job/’ . $id . ‘/’;
    after: $joburl = BASE_URL . URL_JOBS .’/’ . $id . ‘/’;

    #7 – Add URL_JOB to this line:
    before: $joburl = BASE_URL_ORIG . ‘job/’ . $id . ‘/’;
    after: $joburl = BASE_URL_ORIG . URL_JOBS . $id . ‘/’;

    To kick this feature up a notch, I’m going to try to introduce the Twitter API into this code for an automated Tweet of every new job posting.

Leave a Response