return value from php to batch file - php

I have a batch file that calls a php script. The php script returns a value that I want to use in the batch script. The php script echoes the correct return value however I have not been able to find a way to capture the value to use in the batch file.
I have tried a number of variations on for loops without success. My batch script is as follows:
#echo off
Setlocal
"C:\Program Files (x86)\PHP\v5.3\php.exe" -f C:\inetpub\wwwroot\hello.php
endlocal
This of course just returns "Hello." How can I set a variable in the batch file (a variable named retVar for example) to contain what is returned from this php call?
thank you,
Shimon

#echo off
setlocal enableextensions
for /f "delims=" %%a in (
'"C:\Program Files (x86)\PHP\v5.3\php.exe" -f C:\inetpub\wwwroot\hello.php'
) do set "retVar=%%a"
echo %retVar%
Use for /f to execute a command (in the in clause) and for each line in the command output, execute a block of code (in the do clase). For each line, the variable/replaceable parameter (%%a in sample code) will hold the line. In this case, as the output of the php command is only one line, it will be executed only one time, storing the php output line into the retVar variable.
for /f will, by default, try to tokenize the lines it reads, splitting in fields using spaces a delimiters. To avoid this behaviour, a empty list of delimiters is used, so all the line contents will be stored in %%a

Related

Get PHP output on Batch file

Found this nowhere else.
I have a batch file which calls a PHP file (php.exe - f file.php).
I'm blocked as i want to pass the PHP output (0 or 1) to the batch file.
Any idea on how to do this?
Thanks
In PHP write:
<?php
echo 'Done';
exit(0);
// Use a value >= 1 for errors
//exit(1);
Then use the %ERRORLEVEL% variable within the batch file to get the result (error level) from PHP.
#echo off
php.exe -f test.php
echo %ERRORLEVEL%
in the .bat file you can assign any output to the variable like this:
FOR /F "usebackq tokens=*" %%x IN (`php.exe -f test.php`) do (SET "VARIABLE=%%x")
echo result is %VARIABLE%
VARIABLE is an arbitrary variable name inside the batch
usebackq parameter allows to put whole command in the back quotes (`) and use double quotes (") inside as parameters.

How to execute C code through PHP by prompting terminal

I have a C code that I have to execute through PHP,
I have used exec('./sys'), sys is my executable file.
I have also tried system(), passthrough(), shell_exec() and they are not giving output.
When I executed exec('who'); it gives the output.
What can I do to execute sys?
Each of those methods you reference will execute your sys file, but you need to make sure you are executing the correct path. Your working path is determined by what script is actually executing PHP. For example, if you're executing your code through apache or the command line your working directory may be different. Lets assume this file structure:
+ src/
| + script.php
| + sys
I would recommend using PHP's __DIR__ magic variable in your script.php to always reference the current file's directory, and then work from there:
// script.php
exec(__DIR__ . "/sys");
Retrieving output can be done a couple different ways. If you want to store the output of the script in a variable, I would recommend using exec according the the manual:
Parameters ΒΆ
command
The command that will be executed.
output
If the output argument is present, then the specified array will be filled with every line of output from the command. Trailing whitespace, such as \n, is not included in this array. Note that if the array already contains some elements, exec() will append to the end of the array. If you do not want the function to append elements, call unset() on the array before passing it to exec().
return_var
If the return_var argument is present along with the output argument, then the return status of the executed command will be written to this variable.
exec will return the first line of output, but if you want more than that you need to pass a variable by reference:
// script.php
$output = array();
exec(__DIR__ . "/sys", $output);
$output will then contain an array of each line of output from the command. However if you want to run your sys script and directly pass through the output then use passthru(__DIR__ . "/sys"); For example, if you wanted to execute a command that required input on the command line, passthru would be the best option.

Php exec : output without clean the file

I'm calling a php script using exec and I'm trying to make a simple log.
Currently I have this :
exec("php script.php $options > temp/log.txt");
If I execute once the result is wrote, but if I execute this multiple times it's always replaced by the last call.
Is there a way to just add the output at the end of the .txt, without replacing all the file ?
Thanks
This has nothing to do with php, you are looking for a shell feature:
exec("php script.php $options >> temp/log.txt");
Note the double >> in there. It appends the redirection instead of overwriting the target.

How to capture multi-line output for commands issued with PHP exec?

I am trying to execute system commands in PHP and capture the output of those commands where that output covers multiple lines. I am using exec(), but it seems like it only returns a value for commands that generate output on a single line.
For example, if I run date at the command line I get:
Wed May 15 15:07:32 EST 2013
As expected, if I run this command from PHP as an exec using this...
exec("date", $exec_results);
...then the value of $exec_results becomes...
Array ( [0] => Wed May 15 15:07:32 EST 2013 )
However, when I run time from the command line I get this...
real 0m0.000s
user 0m0.000s
sys 0m0.000s
...but when I do it from PHP with this...
exec("time", $exec_results);
... the value of $exec_results is empty:
Array( )
I don't actually need to run date or time in my application but these are just examples of how the single line vs. multi-line output on the command line seems to change what gets back to PHP.
The documents say:
If the output argument is present, then the specified array will be filled with every line of output from the command.
So why is the $exec_results array not being filled with all the lines seen when the time command is run in the command line?
Notes - I have run the command line entries as the apache user to rule out privileges.
This should work for you
ob_start();
passthru("ls -la");
$dat = ob_get_clean();
So here's the full list:
system() => Execute an external program and displays the output.
passthru() => Same than system, but casts the output in binary "as is" from the shell to the PHP output (typically the HTTP response).
exec() => Captures the output and only the last line of the output into a string.
shell_exec() => Same than exec, but capturing full output, not only the last line.
So, my preference: Always use shell_exec() and then do with the full-string whatever you want.
How to test
$ php -a
Interactive mode enabled
php > echo( shell_exec( "echo hello; echo bye" ) );
hello
bye
php >
https://www.php.net/manual/en/function.shell-exec.php
I changed my post for the working solution:
Use the 'script' command of unix to get the result.
you will surely have to remove "extra lines" of the temporary log file.
exec('script -c "time" /tmp/yourfile.txt');
$result = file('/tmp/yourfile.txt');
var_dump($result);
You should always put a full path to your temporary file, anywhere you put it
that's it !

How to run Java program and get output in PHP?

I'd like to run something like (in myProgram.sh):
java -cp whatever.jar com.my.program $1
within PHP and read the output.
So far I have something like:
$processOrderCommand = 'bash -c "exec nohup setsid /myProgram.sh ' . $arg1 . ' > /dev/null 2>&1 &"';
exec($processOrderCommand);
But what I'd really like is to be able to get the output of the java program within the PHP script and not just execute it as another thread.
How can this be done?
You can do this :
exec($processOrderCommand, $output);
From the documentation :
If the output argument is present, then the specified array will be filled with every line of output from the command. Trailing whitespace, such as \n, is not included in this array. Note that if the array already contains some elements, exec() will append to the end of the array. If you do not want the function to append elements, call unset() on the array before passing it to exec().
For a better control on your execution you can take a look at proc_open()
Resources :
php.net - exec()
php.net - proc_open()
The key is that the classpaths need to be absolute within the shell_exec
PHP script.
Or at least that's the only way I could get it to correctly work. Basically it's almost impossible to tell from environment to environment what the relative directory is that the php script is running the JVM.
As well, it helped to put the absolute path location for java, such as usr/.../bin/java

Categories