Let's say I want to fetch contents from a URL using a PHP script.
One way to do that would be to use PHP function such as
echo file_get_contents("http://www.example.com/file.xml");
Another way would be to use UNIX tools such as wget or curl, or any other tool accessible from shell
echo exec("wget http://www.example.com/file.xml");
Is there a significant performance difference between using exec() and PHP build-in functions to achieve same thing, assuming that both UNIX tools and PHP functions have similar implementation and perform with same efficiency?
What exactly happens when you call an exec() function in terms of resources? Does it actually create a new shell session, or does it run on top of the current php shell session?
The exec() function creates a new shell instance, with its own environment variables, so there is a performance hit.
Related
I have complex js file that do some heavy calculation and save result to collection.
This written in js because i wan't to avid data transmission.
currently i pass script to mongo shell in this way :
$mongodb < path_to_script
The script consists of several functions?
Is it possible to execute it from PHP?
I saw there is 'nolock' parameter that can be pass to evel method, is it possible to use it when executing from shell or from PHP?
What is consider more safe , using php execute wrapper or executing script from shell ?
When you run a .js file in the mongo shell, it's not running the javascript on the server; it's running it in the shell. Are you using server-side javascript features like db.eval and map/reduce in the mongo shell script?
Either way, I'd suggest forgoing the shell script and server-side javascript functions and either using the aggregation framework for server-side processing or implementing the logic in PHP application code. Server-side javascript has serious performance and security limitations and it's best to avoid using it when possible.
You can read the js file and execute it in PHP using execute which is a wrapper to the eval command. The nolock option can be added in the arguments.
$code = file_get_contents('filenam.js');
$db->execute($code, array('nolock' => true));
The php execute wrapper should be safer and better solution. If you execute js using shell in PHP, you will need to give permission to php to execute shell script using shell_exec(), which is impossible if you use hosting service and don't have admin right on the host.
I'm writing a command-line application that will substitute a bunch of bash functions and manual work made by a team of developers. Currently, half of what we usually do is inside a ~/.functions file that is sourced in the ~/.bash_profile of each developer.
My command-line application is written in PHP, and for a while I would need to run some of those functions from inside my application. However, the following code would not work, the output says it cannot find the given function:
exec('bash my_legacy_functions.sh');
exec('my_custom_legacy_function param1 param2');
I may be wrong, but I could understand that every exec() call runs a command in a separate process, meaning the functions would not be available for subsequents exec() calls. Is this right, and if yes, would it be possible to override this behaviour without having to bundle everything into one call?
In the end it turns out the default shell was not bash, and on top of that source is not a common command in bash. I found by this other question's answer that the solution is something like:
function run($cmd) {
exec("bash -c 'source my_legacy_functions.sh; $cmd'");
}
It is possible to run an external command by three PHP functions of
system();
exec();
shell_exec();
but what are their differences? In spite of their specific applications, in most cases, the can be equally used. I am curious to know which is preferred one when they can be equally used. For example, for unzipping a file or compressing a folder (with tar command), which one is preferred (probably from performance point of view)?
UPDATE: In another question, I found a very useful link describing different aspects for these functions. I share the link here, as other may use to better understand security issues and other aspects.
exec — Execute an external program
system — Execute an external program and display the output
shell_exec — Execute command via shell and return the complete output as a string
so if you don't need the output, I would go with exec.
Further details:
http://php.net/manual/en/function.exec.php
http://php.net/manual/en/function.system.php
http://php.net/shell_exec
Is there a way to kill a Windows process, say calc.exe, with PHP without using exec?
exec calls an external program, I try to avoid using that command unless necessary.
There is.
Use the W32api PHP extension, which provides access to the Win32 API - then use the TerminateProcess() API after aquiring a handle to the process.
define new function in PHP sources ( pure C )
compile you new PHP version with the defined function
call the new function, which was defined in PHP sources by you
PS
On this way you are able to make PHP able work with __asm { } insertions/add some kernel code , if you want it...
No, there isn't. PHP has little access to the system except through exec.
My question is whether or not Flex's fcsh can be called from within a PHP script. Here is the background:
I have been created a simple process that creates a simple quiz/tutorial by converting a text file into a .mxml file and compiling to a .swf file using the mxmlc compiler. This works well from the command line, but I wanted to make the process easier by creating a web-interface to do this. My initial attempts with PHP's exec() function have not worked. The Python scripts I use to create the .mxml file work fine (using exec()), but I have not been able to get the mxmlc compiler to work.
After searching on the Web and on this site, I believe that using fcsh (instead of mxmlc) may be the way to go. Using fcsh would certainly compile the .mxml file faster (after the first run), and I think that fcsh can be launched as a service that might be able to be called from PHP.
On the other hand, maybe I am approaching this the wrong way. Would it be better to write a Flex application that calls fcsh and avoid using PHP?
Edit: Using fcshctl as hasseg suggested in his answer below worked very well. Thanks Ali.
The problem with calling fcsh from within scripts is that it works as an interactive shell instead of taking command-line arguments, compiling, and returning an exit status. There are different ways to get around this, which I've listed in this blog post of mine, where I mainly talk about fcshctl (which is my own solution for this,) but at the bottom of the post I've also listed other similar solutions to get fcsh integrated into nonstandard build workflows.
There are a few other ways in php to execute an external script. They are exec(), passthru(), system(), and backticks i.e. the key to the left of the 1 key. Each one has a different purpose and return mechanism.
You may have to put the command that executes your executable into a script and call that script via one of these functions.
Is there a particular reason why you can't use mxmlc directly? It seems like it would be easier to call than fcsh. Just specify all your compiler options in a XML file run it like mxmlc -load-config path/to/config.xml. You can find an example of the XML configuration format in FLEX_HOME/frameworks/flex-config.xml.