简体   繁体   中英

Is it possible to call a Codeigniter class function from an executable PHP file?

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM