Welcome Guest, Not a member yet? Register   Sign In
Access Codeigniter functions from external script
#1

[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?
#2

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

[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
#4

[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.
#5

[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.
#6

[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?
#7

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

[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?
#9

[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/11735...nal-script
#10

[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/62734...r-and-exec





Theme © iAndrew 2016 - Forum software by © MyBB