AllDevJobs.com

Want to post new jobs to popular micro-blogging service Twitter? Of course you do. Follow the tutorial below to make your Jobberbase site almost as cool as AllDevJobs.com.



To avoid conflicting with newer versions of Jobberbase, I am writing this tutorial for any version so it should always work. As always, make sure you back up your site before you proceed with the following changes.

Step 1: Create a Twitter Account

This may seem obvious, but yes, you need to create a new account. Create Account »

Step 2: Modify config.php

In the root directory of your Jobberbase installation, you have a ‘config.php’ file. In that file you will want to add your Twitter account information. I added mine at line 41. The bolded text indicates the places you need to add your account info.

/* Twitter username & password */
define('TWITTER_ENABLED',1);
define('TWITTER_USERNAME','username_goes_here');
define('TWITTER_PASSWORD','password_goes_here');

After that’s done, you also need to add a reference to the Twitter function you’ll be creating in Step 4. Around line 87, you should see the following:

// Function and classes includes
require_once '_includes/function.validate_email.php';
require_once '_includes/function.redirect_to.php';
require_once '_includes/function.printr.php';
require_once '_includes/function.escape.php';
require_once '_includes/functions.php';
require_once '_includes/class.phpmailer.php';
require_once '_includes/class.Postman.php';
...

You need to add require_once '_includes/function.Twitter.php'; to this list. I added it just after require_once '_includes/functions.php';.

When done, it should look like this:

// Function and classes includes
require_once '_includes/function.validate_email.php';
require_once '_includes/function.redirect_to.php';
require_once '_includes/function.printr.php';
require_once '_includes/function.escape.php';
require_once '_includes/function.Twitter.php';
require_once '_includes/functions.php';
require_once '_includes/class.phpmailer.php';
require_once '_includes/class.Postman.php';
require_once '_includes/class.Textile.php';
require_once '_includes/class.Sanitizer.php';
require_once '_includes/class.Db.php';
require_once '_includes/class.Job.php';
require_once '_includes/class.JobRequest.php';
require_once '_includes/class.Paginator.php';
require_once '_includes/class.Feed.php';
require_once '_includes/class.SpamReport.php';
require_once '_includes/class.Api.php';
require_once '_includes/class.JobApplication.php';
require_once '_includes/class.SearchKeywords.php';
require_once '_includes/smarty/libs/Smarty.class.php';

Step 3: Edit file ‘_includes/class.Job.php’

You will shortly be creating a new Twitter function that will post the job listings for you, however, you need to find the right place to call that function, which would be immediately after the job listing is activated, not necessarily when it’s been posted, because you don’t want people just posting anything to Twitter before you’ve reviewed it.

Around line 873, you’ll need to replace the entire public function Publish() with the following code:


	// Publishes a newly created job post (is_temp => 0)
	public function Publish()
	{
		global $db;
		if ($this->CheckPosterEmail())
		{
			$sql = 'UPDATE jobs SET is_temp = 0, is_active = 1 WHERE id = ' . $this->mId;
			$db->query($sql);

			$sql = 'SELECT type_id, id, title, company FROM jobs WHERE id = ' . $this->mId;
			$result = $db->query($sql);
			$row = $result->fetch_assoc();

		  $formattedMessage = $row['title'] . " at " .$row['company'] . " " . BASE_URL . "job/" .$row['id'] . "/";
		  Tweet($formattedMessage);

		}
		else
		{
			$sql = 'UPDATE jobs SET is_temp = 0, is_active = 0 WHERE id = ' . $this->mId;
			$db->query($sql);
		}

	}

Step 4: Create file ‘_includes/function.Twitter.php’

In your main includes directory, you need to create a new file for the script to post Twitter messages. You will need to have cURL enabled on your server for this to work.

Inside the new file, add this:

<?php
/**
 * Twitter post API
 */

function Tweet($msg){
	if(TWITTER_ENABLED==1){

		$username = TWITTER_USERNAME;
		$password = TWITTER_PASSWORD; 

		$message = $msg;

		$url = 'http://twitter.com/statuses/update.xml';
		// Alternative JSON version
		// $url = 'http://twitter.com/statuses/update.json';

		$curl_handle = curl_init();
		curl_setopt($curl_handle, CURLOPT_URL, "$url");
		curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
		curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($curl_handle, CURLOPT_POST, 1);
		curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "status=$message");
		curl_setopt($curl_handle, CURLOPT_USERPWD, "$username:$password");
		$buffer = curl_exec($curl_handle);
		curl_close($curl_handle);
		// check for success or failure
		if (empty($buffer)) {
				echo 'Post failed. Please add listing manually.';
		} else {
				echo 'Success';
		}
	}
}

?>

That’s it. Upload your files and you’re ready to post to Twitter. Please let me know if you have any difficulties with this tutorial and I will update it accordingly.

Bookmark or share this:

  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Design Float
  • E-mail this story to a friend!
  • NewsVine
  • Ping.fm
  • Print this article!
  • Reddit
  • StumbleUpon
  • Technorati
  • Tumblr
  • TwitThis
  • Yahoo! Buzz