Not a dupe, in this very case I am trying to run my php thru a command line and it does not work as expected (php uses php://input and I do not want to modify them)
I am trying the following:
echo 'hello' | php -r 'print("input content:".file_get_contents("php://input"));'
I am expecting something like
content:hello
to be printed out , but get only
content:
Please try with stdin instead:
echo "hello" | php -r 'print_r("input content:".file_get_contents("php://stdin"));'
More about CLI streams: http://php.net/manual/en/features.commandline.io-streams.php
You can use the arguments.
#!/usr/bin/php
<?php
// loop through each element in the $argv array
foreach($argv as $value){
echo "$value\n";
}
In fact php://input will only work if I use php-cgi instead of php
So to pass a request body code is like:
echo '{"a":1}' | php-cgi -r 'print("input
content:".file_get_contents("php://input"));'
I have a string in the following format :
(str_rot13(base64_decode("my string")))
I am trying to decode it using a single linux command by piping the output of Base64 decode to rot13.
I am attempting to use echo 'my string' | base64 --decode then pipe the output to tr 'n-za-mN-ZA-M' ‘a-zA-Z’ which applies the Rot13 decode operation on the output.
Can you guide me for the best possible way I can do it using the command line.
Edit
Apologies guys, I was looking at the partial script. I just noticed that the complete script is something like this:
<?php eval(gzinflate(str_rot13(base64_decode('my string')))); ?>
You should be able to pipe those two commands straight through (the only possible issue I can see are the ‘curly’ quotes in your tr command).
PHP
php > echo str_rot13(base64_decode("c2JiCg=="));
foo
Bash
echo 'c2JiCg==' | base64 --decode | tr 'n-za-mN-ZA-M' 'a-zA-Z'
foo
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 can I convert/decode html entities of a file's contents (XML) in PHP.
I tried to run this on a command line:
perl -MHTML::Entities -ne 'print decode_entities($_)' /apps/www/mydir/xmlfiles/p34580600.xml >> /apps/www/mydir/xmlfiles/p34580600_1.xml
It works fine running it on command line but when I try to call it within PHP:
system("perl -MHTML::Entities -ne 'print decode_entities($_)' /apps/www/mydir/xmlfiles/p34580600.xml >> /apps/www/mydir/xmlfiles/p34580600_6.xml");
It creates the file but it is empty. I tried to use html_entity_decode but the XML file is just too big. 20megs at least.
Any help or suggestion is greatly appreciated.
Thanks,
try escaping the $ in the system call. It is possible php is looking for a variable $_ since you are using double quotes for the string command.
It is possible to pipe data using unix pipes into a command-line php script? I've tried
$> data | php script.php
But the expected data did not show up in $argv. Is there a way to do this?
PHP can read from standard input, and also provides a nice shortcut for it: STDIN.
With it, you can use things like stream_get_contents and others to do things like:
$data = stream_get_contents(STDIN);
This will just dump all the piped data into $data.
If you want to start processing before all data is read, or the input size is too big to fit into a variable, you can use:
while(!feof(STDIN)){
$line = fgets(STDIN);
}
STDIN is just a shortcut of $fh = fopen("php://stdin", "r");.
The same methods can be applied to reading and writing files, and tcp streams.
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
If your data is on one like, you can also use either the -F or -R flag (-F reads & executes the file following it, -R executes it literally) If you use these flags the string that has been piped in will appear in the (regular) global variable $argn
Simple example:
echo "hello world" | php -R 'echo str_replace("world","stackoverflow", $argn);'
You can pipe data in, yes. But it won't appear in $argv. It'll go to stdin. You can read this several ways, including fopen('php://stdin','r')
There are good examples in the manual
This worked for me:
stream_get_contents(fopen("php://stdin", "r"));
Came upon this post looking to make a script that behaves like a shell script, executing another command for each line of the input... ex:
ls -ln | awk '{print $9}'
If you're looking to make a php script that behaves in a similar way, this worked for me:
#!/usr/bin/php
<?php
$input = stream_get_contents(fopen("php://stdin", "r"));
$lines = explode("\n", $input);
foreach($lines as $line) {
$command = "php next_script.php '" . $line . "'";
$output = shell_exec($command);
echo $output;
}
If you want it to show up in $argv, try this:
echo "Whatever you want" | xargs php script.php
That would covert whatever goes into standard input into command line arguments.
Best option is to use -r option and take the data from the stdin. Ie I use it to easily decode JSON using PHP.
This way you don't have to create physical script file.
It goes like this:
docker inspect $1|php -r '$a=json_decode(stream_get_contents(STDIN),true);echo str_replace(["Array",":"],["Shares"," --> "],print_r($a[0]["HostConfig"]["Binds"],true));'
This piece of code will display shared folders between host & a container.
Please replace $1 by the container name or put it in a bash alias like ie displayshares() { ... }
I needed to take a CSV file and convert it to a TSV file. Sure, I could import the file into Excel and then re-export it, but where's the fun in that when piping the data through a converter means I can stay in the commandline and get the job done easily!
So, my script (called csv2tsv) is
#!/usr/bin/php
<?php
while(!feof(STDIN)){
echo implode("\t", str_getcsv(fgets(STDIN))), PHP_EOL;
}
I chmod +x csv2tsv.
I can then run it cat data.csv | csv2tsv > data.tsv and I now have my data as a TSV!
OK. No error checking (is the data an actual CSV file?), etc. but the principle works well.
And of course, you can chain as many commands as you need.
If you are wanting more to expand on this idea, then how about the ability to include additional options to your command?
Simple!
#!/usr/bin/php
<?php
$separator = $argv[1] ?? "\t";
while(!feof(STDIN)){
echo implode($separator, str_getcsv(fgets(STDIN))), PHP_EOL;
}
Now I can overwrite the default separator from being a tab to something else. A | maybe!
cat data.csv | csv2tsv '|' > data.psv
Hope this helps and allows you to see how much more you can do!