Writing to stdin from PHP?
Your first executes the command with a copy of the current script's stdin, not the text you provide.
Your second fails because you are forgetting the newline. Try fwrite($handle, "10\n")
instead. Note that zenity seems to jump to 100% when EOF is reached (e.g. by the implicit close of $handle
at the end of your PHP script).
Your third fails because you are forgetting the newline and you are writing to the wrong pipe. Try fwrite($pipes[0], "10\n")
instead, and remember the same note regarding EOF as above.
PHP standard input?
It is possible to read the stdin
by creating a file handle to php://stdin
and then read from it with fgets()
for a line for example (or, as you already stated, fgetc()
for a single character):
<?php
$f = fopen( 'php://stdin', 'r' );
while( $line = fgets( $f ) ) {
echo $line;
}
fclose( $f );
?>
How to process stdin to stdout in php?
If you are piping, you will want to buffer the input, instead of processing it all at once, just go one line at a time as is standard for *nix tools.
The SheBang on top of the file allows you to execute the file directly, instead of having to call php in the command line.
Save the following to test.php and run
cat test.php | ./test.php
to see the results.
#!php
<?php
$handle = fopen('php://stdin', 'r');
$count = 0;
while(!feof($handle)) {
$buffer = fgets($handle);
echo $count++, ": ", $buffer;
}
fclose($handle);
proc_open not writing to STDIN on simple C program
Figured out:
You need to specify the buffer for freads on windows it seems:
echo fread($pipes[1], 1024);
How to execute an external program, sending data to STDIN and capturing STDOUT?
For generic use for console commands, what you are after is shell_exec()
- Execute command via shell and return the complete output as a string
Note that for your use case, the OpenSSL functions that PHP provides may do what you need to do "natively" without worrying about spawning a shell (often disabled for security reasons)
Anyway, back to shell_exec():
cat in.pkcs7 | openssl pkcs7 -inform DER > out.pkcs7
could be done as
// obtain input data somehow. you could just
// shell_exec your original command, using correct
// paths to files and then read out.pkcs7 with file_get_contents()
$infile=file_get_contents("/path/to/in.pcks7");
$result=shell_exec("echo ".$infile." | openssl pcks7 - inform DER");
May/will/does need tweeking with "
and '
to handle special characters in the $infile
content. Change $infile
to be whatever input you'd be sending anyway, just be sure to quote/escape properly if needed.
Related Topics
Using X-Sendfile with Apache/Php
How to Access an Object Attribute That Starts with a Number
How to Find Unused Functions in a PHP Project
PHP Check Whether Property Exists in Object or Class
Upload 1Gb Files Using Chunking in PHP
Caching Http Responses When They Are Dynamically Created by PHP
Doing Http Requests from Laravel to an External API
Can't Install Laravel Installer via Composer
Generating Random Numbers Without Repeats
Getting Http Code in PHP Using Curl
.Htaccess Rewriterule to Preserve Get Url Parameters
How to Check If a Url Exists in PHP
Mcrypt_Encrypt to Openssl_Encrypt, and Openssl_Zero_Padding Problems