@@ -28,6 +28,8 @@ composer require mikehaertl/php-shellcommand
2828 * Catches ` stdOut ` , ` stdErr ` and ` exitCode `
2929 * Handle argument escaping
3030 * Pass environment vars and other options to ` proc_open() `
31+ * Pipe resources like files or streams into the command
32+ * Timeout for execution
3133
3234## Examples
3335
@@ -49,7 +51,69 @@ if ($command->execute()) {
4951
5052### Advanced Features
5153
54+ #### Add Arguments
5255``` php
56+ <?php
57+ $command = new Command('/bin/somecommand');
58+ // Add arguments with correct escaping:
59+ // results in --name='d'\''Artagnan'
60+ $command->addArg('--name=', "d'Artagnan");
61+
62+ // Add argument with several values
63+ // results in --keys key1 key2
64+ $command->addArg('--keys', array('key1','key2'));
65+ ```
66+
67+ ### Pipe Input Into Command
68+
69+ From string:
70+ ``` php
71+ <?php
72+ $command = new ('jq') // jq is a pretty printer
73+ $command->setStdIn('{"foo": 0}');
74+ if (!$command->execute()) {
75+ echo $command->getError();
76+ } else {
77+ echo $command->getOutput();
78+ }
79+ // Output:
80+ // {
81+ // "foo": 0
82+ // }
83+ ```
84+
85+ From file:
86+ ``` php
87+ <?php
88+ $fh = fopen('test.json', 'r');
89+ // error checks left out...
90+ $command = new Command('jq');
91+ $command->setStdIn($fh);
92+ if (!$command->execute()) {
93+ echo $command->getError();
94+ } else {
95+ echo $command->getOutput();
96+ }
97+ fclose($fh);
98+ ```
99+ From URL:
100+ ``` php
101+ <?php
102+ $fh = fopen('https://api.open-meteo.com/v1/forecast?latitude=52.52&longitude=13.41&hourly=temperature_2m,relativehumidity_2m,windspeed_10m', 'r');
103+ // error checks left out...
104+ $command = new Command('jq');
105+ $command->setStdIn($fh);
106+ if (!$command->execute()) {
107+ echo $command->getError();
108+ } else {
109+ echo $command->getOutput();
110+ }
111+ fclose($fh);
112+ ```
113+
114+ #### Set Command Instance Options
115+ ``` php
116+ <?php
53117// Create command with options array
54118$command = new Command(array(
55119 'command' => '/usr/local/bin/mycommand',
@@ -64,17 +128,6 @@ $command = new Command(array(
64128 'bypass_shell' => true,
65129 ),
66130));
67-
68- // Add arguments with correct escaping:
69- // results in --name='d'\''Artagnan'
70- $command->addArg('--name=', "d'Artagnan");
71-
72- // Add argument with several values
73- // results in --keys key1 key2
74- $command->addArg('--keys', array('key1','key2'));
75-
76- // Add string to pipe to command on standard input
77- $command->setStdIn('string');
78131```
79132
80133## API
0 commit comments