Access Codeigniter functions from external script - php

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 (e.g. $this->db or $CI->db - doesn't matter) from that external PHP script?
Idea : Could a workaround, like include 'index.php'; at the top of the script do the trick?

In recent Codeigniter versions you can run standard controller actions from command line.
If your task is some kind of a daemon you can embed the "task trigger" logic in a long running part of your script and shell out for the actual work to CI land.

Related

Codeigniter running background scripts with same configuration and libraries

Fellows,
I want when I do a fileupload on codeigniter when upload finished to run a background process.
The background process will read the file and will insert data on my db. What I want is to the background script (that will be written in php) to be able to use codeignter's configuration and query builder. Because codeingiter's methods are more convenient than uusinhg vanila php.
I know to I can execute stuff on the background via exec and that's now what I am asking. I am asking how the executed script can use codeigniter's methods and libraries.
Check out the documentation on Running via the CLI. It may be possible to pass what they show as a terminal command as the argument to exec and achieve what you want.

How do I run a php function in a subprocess in Symfony2?

I want to run some functions parallel in the background.
Is there a nice way to do this in Symfony2 or even with php?
Or is there only the Symfony\Component\Process\Process?
Can I use this Process with a function? I need the actual Context (logged in user and some session data), so it is not possible to source the function out to an external php-file...
Symfony2 Process component allows you to run some shell command or execute php-script in a different process.
To run exact function in a thread try look at PHP Thread class

Writing a CLI for a php-based CMS (Similar to Drush)

I've been working on a CMS for a few years and I actually implemented a jquery-based console in the admin area, where you could do some handy things like enable/disable modules and so on.
I recently fiddled around with drupal and decided to install cygwin along with drush.
I tried googling around but figured this might be an unusual question: How does one go about creating a CLI for a php-based CMS? And exactly how does exactly drush worK? I mean, I know that it runs from the command line as a batch script in windows. But how does it interact with PHP and so on?
I do know some basic C# but this shouldn't be very hard once i figure out how this fits together. (php, sql, etc).
Any help is appreciated, thanks in advance :)
You can run a php cli from the terminal only when you have php compiled with cli support. Additional you need to specify an interpreter and pass the path to the script as argument. But you can also use a shebang #!/path/to/php. A better practise would be to use the env variable and not hardcode the path to php: #!/usr/bin/env php. Read here about it: http://tech.vg.no/2012/02/21/dont-hardcode-php-executable-path-in-shebang/.
Basically you can write a simple CLI shell with a infinite loop plus a 'exec()' or 'shell_exec()' PHP functions. you should get the user commands and send it to shell_exec() function for execute in a system shell and return the output of that to the user.
i.e:
while(TRUE){
if($input != 'exit')
$output=shell_exec($input);
else
break;
echo $output;
}
you can add other options and customize this simple loop.
you can call external programs with 'exec()' function.

From php how the system() function is working?

I am working in a project, where the script will run under Linux. It has many modules, which are written in C++. I need to call these modules from PHP.
My problem is as follows:
My module is one of the module in the software package.
Our software is having the PHP layers to take user input and store it in database, and also call the C++ engine when needed.
All modules are running and using some environment variable which was set by the base module.
There is one layer of PHP through we are getting the user inputs to our C++ engines.
I need to call an application (abc.out) from PHP and it will fill the database.
The problem is that I have to set one new environment variable before the application will work.
I am getting the old environment variable using getenv() then appending a path to it and setting again. I have used putenv() to set the new environment variable.
After setting the environment variable I am using the system() to call that application from PHP and it is working.
My doubt is whether this environment variable change will affect other modules those are running.
One thing is that if we use system() for multiple operations, I can set the environment variable and call my application both from the same system() call. Here my doubt is whether this will also affect the other application which are running, or only the particular application which being called with system().
That is system() is a creating a new session for each program what we call by it or not?
Thanks
Well, php is open source :-) You need to look in the file ext/standard/exec.c. system is implemented as a call to php_exec_ex, which via php_exec uses a macro called VCWD_POPEN, which on a Linux system uses a popen() system call.
So, each call to PHP system() on a Linux system will be executed through a fresh popen() system call, so yes, the separate calls are isolated. I didn't check for Windows.

How to run a command line script through a normal Zend Framework application

I am working on a ZF application that needs to run a command line script and then parse the results into something meaningful and return to the user.
I know there are various PHP functions, like exec and system, but I was wondering if there is anything built into Zend Framework that does command line scripting easily.
Even if there isn't a ZF specific function, what is the best function/method to use for running a command line script and then retrieving the results in PHP upon completion of the script.
I would write a Service which uses exec to get what you need. You can add some basic error handling there.
You can start an process under linux with this:
ZendX_Console_Process_Unix
But never tryed it..
We recently needed to do this (we use ZF too), hope this helps: http://www.kintek.com.au/web-design-blog/how-to-run-a-php-script-from-command-line/

Categories