CodeIgniter Forums
Access Codeigniter functions from external script - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Access Codeigniter functions from external script (/thread-53597.html)

Pages: 1 2


Access Codeigniter functions from external script - El Forum - 07-31-2012

[eluser]drkameleon[/eluser]
Hi guys! I'm new to CI, so this question may sound silly, but anyway...

So, here's my situation :

- I have a complete app set up with CodeIgniter.
- I'm initiating a background task, running a PHP script.

My question :

- How could I use CodeIgniter function/object (e.g.
Code:
$this->db
or
Code:
$CI->db
- doesn't matter) from that external PHP script?

Basically, what I mainly want to do now is access the db.

----

Idea : Could a workaround, like include 'index.php'; at the top of the script do the trick?


Access Codeigniter functions from external script - El Forum - 07-31-2012

[eluser]Aken[/eluser]
Why can't your background task be part of your CI app?


Access Codeigniter functions from external script - El Forum - 07-31-2012

[eluser]drkameleon[/eluser]
[quote author="Aken" date="1343722036"]Why can't your background task be part of your CI app?[/quote]

Well, I'm all ears if you have any suggestion on how I could do this.

Here's what I'm basically trying to do (=current approach) :

- from page A : user completes a form and initiates an ajax request.
- this ajax request sends all form input to an intermediate script (let's call it interm.php)
- interm.php initiates a background task (php script) - let's call it background.php and returns a tracking number.

e.g.

Code:
$command = "/usr/local/bin/php /public_html/r/path_to_baackground.php $params > /dev/null &";
exec($command);

What I want to do is :

- have background.php running in the background and update a tracking value (somewhere in the db?)
- keep sending ajax requests from page A, to some progress-tracking script, and show the progress
- when background.php has finished, show the results


Access Codeigniter functions from external script - El Forum - 07-31-2012

[eluser]Aken[/eluser]
I guess that depends on what your "background.php" script is supposed to do. I was under the impression you were just trying to run a cron or something to a particular URL. I don't mess around with exec() myself.


Access Codeigniter functions from external script - El Forum - 07-31-2012

[eluser]astroanu[/eluser]
well you cant access the codeigniter functions from an external php script. You can use raw php/mysql scripting but i wouldn't recommend that.

why cant you send the request to background.php directly? without going thru interm.php?

--

if you want to track something (execute code upon a CI event) there's something called hooks in CI. Maybe it'll work for you.


Access Codeigniter functions from external script - El Forum - 07-31-2012

[eluser]drkameleon[/eluser]
[quote author="astroanu" date="1343732379"]
why cant you send the request to background.php directly? without going thru interm.php?
[/quote]

How can I send the request directly, given that background.php has to be executed as a background task?


Access Codeigniter functions from external script - El Forum - 07-31-2012

[eluser]astroanu[/eluser]
If its not a cronjob then cant you add that code to the interm.php?


Access Codeigniter functions from external script - El Forum - 07-31-2012

[eluser]drkameleon[/eluser]
[quote author="astroanu" date="1343734063"]If its not a cronjob then cant you add that code to the interm.php? [/quote]

The point is that what I'm going to include in that background.php is going to take from 1-2 secs to 2-3 minutes. During that time, I want the user to see some updates on the progress. If add that to interm.php, the ajax request will not return until the task is over + I may face script timeout issues... Any suggestions?


Access Codeigniter functions from external script - El Forum - 07-31-2012

[eluser]drkameleon[/eluser]
Guys, thanks a lot for your suggestions. :-)

Here's how I solved it (based on an idea on StackOverflow) :

http://stackoverflow.com/questions/11735455/access-codeigniter-functions-from-external-script


Access Codeigniter functions from external script - El Forum - 07-31-2012

[eluser]astroanu[/eluser]
okay now i get it.

assuming background.php is not accessing the db the easiest way is to take the script in interm.php in to the CI application.

OR you might wanna read this http://stackoverflow.com/questions/6273483/code-igniter-and-exec