How to Pass Variables as Stdin into Command Line from PHP

How to pass variables as stdin into command line from PHP

I'm not sure about what you're trying to achieve. You can read stdin with the URL php://stdin. But that's the stdin from the PHP command line, not the one from pdftk (through exec).

But I'll give a +1 for proc_open()


<?php

$cmd = sprintf('pdftk %s fill_form %s output -','blank_form.pdf', raw2xfdf($_POST));

$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
2 => null,
);

$process = proc_open($cmd, $descriptorspec, $pipes);

if (is_resource($process)) {
// $pipes now looks like this:
// 0 => writeable handle connected to child stdin
// 1 => readable handle connected to child stdout

fwrite($pipes[0], stream_get_contents(STDIN)); // file_get_contents('php://stdin')
fclose($pipes[0]);

$pdf_content = stream_get_contents($pipes[1]);
fclose($pipes[1]);

// It is important that you close any pipes before calling
// proc_close in order to avoid a deadlock
$return_value = proc_close($process);

header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="output.pdf"');
echo $pdf_content;
}
?>

Pass a variable to a PHP script running from the command line

The ?type=daily argument (ending up in the $_GET array) is only valid for web-accessed pages.

You'll need to call it like php myfile.php daily and retrieve that argument from the $argv array (which would be $argv[1], since $argv[0] would be myfile.php).

If the page is used as a webpage as well, there are two options you could consider. Either accessing it with a shell script and Wget, and call that from cron:

#!/bin/sh
wget http://location.to/myfile.php?type=daily

Or check in the PHP file whether it's called from the command line or not:

if (defined('STDIN')) {
$type = $argv[1];
} else {
$type = $_GET['type'];
}

(Note: You'll probably need/want to check if $argv actually contains enough variables and such)

How can pass the value of a variable to the standard input of a command?

Simple, but error-prone: using echo

Something as simple as this will do the trick:

echo "$blah" | my_cmd

Do note that this may not work correctly if $blah contains -n, -e, -E etc; or if it contains backslashes (bash's copy of echo preserves literal backslashes in absence of -e by default, but will treat them as escape sequences and replace them with corresponding characters even without -e if optional XSI extensions are enabled).

More sophisticated approach: using printf

printf '%s\n' "$blah" | my_cmd

This does not have the disadvantages listed above: all possible C strings (strings not containing NULs) are printed unchanged.

Howto pass a mixed string from php through a pipe to a bash script

I don't know PHP but bash should do something like one post of the top-related on the right side:

#!/usr/bin/env bash

declare line ; line=
declare -a line_ ; line_=()

while IFS= read -r line ; do
line_+=( "${line}" )
done < /dev/stdin

printf "%s\n" "${line_[@]}"

Assumed the script's name is some_script.sh you can do

% echo '&Ω↑ẞÐĦØđ¢ø' | bash some_script.sh
&Ω↑ẞÐĦØđ¢ø

while IFS= read -r line is explained here: Bash, read line by line from file, with IFS
and on the bash wiki

  • IFS is set to the empty string to prevent read from stripping leading and trailing whitespace from each line. – Richard Hansen
  • -r raw input - disables interpretion of backslash escapes and line-continuation in the read data

Using STDIN & STDOUT using shell_exec or exec anything else for running a python file from command line wih input passing

Finally I found the solution. The best way to do so is using proc_open()

The code sample is given below.

$descriptorspec = array(
0 => array("pipe", "r"), //input pipe
1 => array("pipe", "w"), //output pipe
2 => array("pipe", "w"), //error pipe
);
//calling script with max execution time 15 second
$process = proc_open("timeout 15 python3 $FileName", $descriptorspec, $pipes);
if (is_resource($process)) {
fwrite($pipes[0], "2"); //sending 2 as input value, for multiple inputs use "2\n3" for input 2 & 3 respectively
fclose($pipes[0]);
$stderr_ouput = [];
if (!feof($pipes[2])) {
// We're acting like passthru would and displaying errors as they come in.
$error_line = fgets($pipes[2]);
$stderr_ouput[] = $error_line;
}

if (!feof($pipes[1])) {
$print = fgets($pipes[1]); //getting output of the script
}
}

proc_close($process);

piping data into command line php?

As I understand it, $argv will show the arguments of the program, in other words:

php script.php arg1 arg2 arg3

But if you pipe data into PHP, you will have to read it from standard input. I've never tried this, but I think it's something like this:

$fp = readfile("php://stdin");
// read $fp as if it were a file

PHP Exec command - How to pass input to a series of questions

First up, just to let you know that you're trying to reinvent the wheel. What you're really looking for is expect(1), which is a command-line utility intended to do exactly what you want without involving PHP.

However, if you really want to write your own PHP code you need to use proc_open. Here are some good code examples on reading from STDOUT and writing to STDIN of the child process using proc_open:

  • http://www.php.net/manual/en/function.proc-open.php#79665
  • How to pass variables as stdin into command line from PHP
  • http://camposer-techie.blogspot.com/2010/08/ejecutando-comandos-sobre-un-programa.html (this one is in Spanish, sorry, but the code is good)

Finally, there is also an Expect PECL module for PHP.

Hope this helps.



Related Topics



Leave a reply



Submit