0

Is it possible to call a Codeigniter class function from an executable PHP file? I have a controller class and I want to call a function of that class from an executable PHP file.

Why? Because my application works with cronjobs. When I create a cronjob from the GUI, a new php file is created and set with the configuration to call the controller function.

How can I do it?

Edit:

I've tried this example, but It doesn't work. The cronjobs created are (combinations to test):

* * * * * /usr/bin/php /home/myweb/public_html/CI/application/controllers/tools message
* * * * * /usr/bin/php http://www.myweb.com/CI/site/index.php/tools/message
* * * * * /usr/bin/php index.php tools message
* * * * * php index.php tools message

But they don't work

Edit:

After the Ben's answer, I've tried (I have a site folder):

* * * * * /usr/bin/php /home/myweb/public_html/CI/index.php tools message
* * * * * /usr/bin/php /home/myweb/public_html/CI/site/index.php tools message

I obtain the next log errors:

ERROR - 2013-01-30 05:52:01 --> Severity: Notice  --> Undefined index:  REMOTE_ADDR /home/myweb/public_html/CI/system/core/Input.php 351
ERROR - 2013-01-30 05:52:01 --> Severity: Warning  --> Cannot modify header information - headers already sent by (output started at /home/myweb/public_html/CI/system/core/Exceptions.php:185) /home/myweb/public_html/CI/system/libraries/Session.php 675
ERROR - 2013-01-30 05:52:01 --> Severity: Warning  --> Cannot modify header information - headers already sent by (output started at /home/myweb/public_html/CI/system/core/Exceptions.php:185) /home/myweb/public_html/CI/system/helpers/url_helper.php 542

[Solved] I've solved it with curl:

* * * * * /usr/bin/curl http://www.myweb.com/CI/site/index.php/tools/message

2 Answers 2

1

Yes it is possible. Straight from the CI documentation...

http://ellislab.com/codeigniter/user-guide/general/cli.html

Edit from your question update...

A couple of issues with your cron...

Cron 1) You are using absolute path to the controller file, should be the absolute path to the index.php

Cron 2) If you want to use the http address, then you should be using curl -L --silent "www.example.com"

Cron 3) Not an absolute document path

Cron 4) should use the absolute path for both php and the index.php

Sign up to request clarification or add additional context in comments.

2 Comments

I've tried: www.myweb.com/index.php/tools/message from the browser and it works, but not with cronjobs. any idea?
I've edited my question about your answer. The error in Input file is in ip_address() function
0

Yes you can. just make sure all the correct files are included in the context that you want.

Create a "new" CodeIgniter Object from your classes, and then use the functions.

3 Comments

Is it executable a class? I'm trying execute a php file, no class (<?php require_once("control.php"); $test = new Control(); $test->cronJobs(); ?>) . Control is a controller class. But It doesn't work
@vicenrele just follow what it says here: ellislab.com/codeigniter/user-guide/general/cli.html
I've tried: www.myweb.com/index.php/tools/message from the browser and it works, but not with cronjobs. any idea?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.