I know that this question is old and has already been answered, but for myself and others, the selected answer does not work. What I've found to work is just using wget. Before you down vote me because you're looking for something that doesn't allow external visitors, hear me out!
If you use wget in conjunction with $this->input->ip_address(); you can ensure that the only machine accessing your script is your own webserver. It isn't as good as being able to call the file locally using php index.php controller function, but it is a fallback.
Here's what I have created which has worked for a few months without issue:
Create a directory somewhere on your server that you can just use as a scratch pad for the temporary files created by wget. I created a folder named cron one level below my public_html folder.
ex. /home/myuser/cron
Construct your cron command. You can string together commands using &&.
i. cd /home/myuser/cron && -- move to your scratch directory
ii. wget http://www.site.com/cron/foo && -- wget your file
iii. rm -f foo -- Remove the downloaded file "foo" from your scratch directory
Your final command will look something like this:
cd /home/myuser/cron && wget http://www.site.com/cron/foo && rm -f foo
Check that the IP address accessing your cron files equals your webserver's IP:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Cron extends MY_Controller {
public function __construct()
{
parent::__construct();
// this controller can only be called from the IP xxx.xxx.xxx.xxx
if ($this->input->ip_address() !== 'xxx.xxx.xxx.xxx'){
show_error('Direct access is not allowed');
}
}
function foo($bar = 'bar')
{
echo $this->input->ip_address();
}
}
Important: Please make sure that you fully understand the effects of rm -f. It can have interesting consequences if you do not supply the correct file. If you have spare time, you could opt out of removing the file and just manually remove all of the cron scratch files periodically.