Make a cron job with IFTTT

Cron is a software utility, a time-based job scheduler in Unix-like computer operating systems. People who set up and maintain…

Novembre 12, 2013

Cron is a software utility, a time-based job scheduler in Unix-like computer operating systems. People who set up and maintain software environments use cron to schedule jobs to run periodically at fixed times or dates.

Sometimes, as a developer, I don’t have access to the server at a deep level and, for example on some basic shared servers, I can’t create cron jobs. A trick to create a cron jobs without server cron program is to use an external service to call your scripts.

For example, you can make it with IFTTT a famous web site that lets you make very astonishing things mixing web services from different sites.
IFTTT works with triggers that are fired on events and make actions.
Some examples: if there is a new item in a rss feed, then send me an email. If tomorrow weather forecast is rain, post a tweet with “take umbrella!”. If I upload a new photo on Instagram with hashtag #Facebook post it to my Facebook profile.
…and many more.

One great missing action is the action of calling a custom url. I don’t know why since this could help many developers to build even more custom actions with IFTTT, such as a cron job that call a php that performs some updates in the database.

Fortunately there is a trick to set up a cron job with IFTTT, throught the Dropbox channel.

So you must connect Dropbox to IFTTT by adding the Dropbox channel and then use the “Add file from url” action to call your php. Mixing this action with the date/time trigger can build a cronjob like cron on unix system.

Here is how you can do it within IFTTT panel. Remember you need to activate the Date & Time channel to use the trigger and also the Dropbox channel to use the save url action that calls your php.

Content of iftttcron.php called for “fake” download in Dropbox channel, the php send headers to emulate a download of some txt data (you will find these files inside your dropbox account and sometimes you have to delete them):

header('HTTP/1.1 200 OK');
header('Content-type: text/plain');
header('Content-disposition: attachment; filename=delete-me.txt');
echo "ok";
// --------------- action -------
// do something...
mail("youremail@yourdomain.com","Hello new!","Send at ".date("Y-m-d H:i:s").". bye.","From:info@yourdomain.com");

UPDATE 11/17/2013
I’ve noticed that IFTTT makes multiple calls, not only one. This results in sending multiple emails and that’s BAD!
So, here is a trick to create a mini caching system for your iftttcron.php file. Before executing the action, check if there is a fresh cached version, if the cached version is old make actions and create the new cache file:

header('HTTP/1.1 200 OK');
header('Content-type: text/plain');
header('Content-disposition: attachment; filename=delete-me.txt');

include("demo/minibots/minibots.class.php");

$output = "ok";

$cache_file_name = "iftttcron.txt";
$age_in_minutes = round((time() - @filemtime($cache_file_name))/60);
if ($age_in_minutes < 60) {
	// output from old cached file
	echo file_get_contents($cache_file_name);
} else {
	// file older than 1 hour
	// --------------- action -------
	// do something...
	mail("youremail@yourdomain.com","Hello new!","Send at ".date("Y-m-d H:i:s").". bye.","From:info@yourdomain.com");
	
	//
	// overwrite old cached file
	// with new data
	$f = fopen($cache_file_name,"w");
	fwrite($f,$str);
	fclose($f);

	//
	// output results
	echo $output;

}

This should prevent multiple emails.

Author

PHP expert. Wordpress plugin and theme developer. Father, Maker, Arduino and ESP8266 enthusiast.

Recommended

How to read facebook likes count from PHP

When you add facebook like button to your site, probably, you also want to save the number of likes of…

Ottobre 8, 2012

How to use Instagr.am photos on your site

UPDATE: 2013-12-04 I’ve made a method in the Mini Bots PHP Class that lets you retrieve images from instagram without…

Agosto 18, 2011

Find values recursively inside complex json objects in PHP

A PHP function to to quickly search complex, nested php structures for specific values.

Dicembre 18, 2022

How to add rel=”nofollow” to links with preg_replace()

Adding rel="nofollow" to external link is a good SEO practice.

Settembre 22, 2015

Limit the number of categories for posts in WordPress

CHOOSE ONLY ONE CATEGORY WORDPRESS If you need to limit the number of categories used by the authors of your…

Settembre 14, 2015

Scraping content with PHP as if it was jQuery

Building a spider or a bot needs some knowledge of regular expressions, you must know and use preg_match or preg_match_all…

Dicembre 8, 2013