Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can pass data to PHP scripts running with the CLI as command line arguments. This data will be available to the child script in the <code>$argv</code> array.</p> <p>input.php:</p> <pre><code>$arg = "Data!"; exec("/usr/bin/php /path/to/data.php ".escapeshellarg($arg)." &gt; /dev/null 2&gt;&amp;1 &amp;"); </code></pre> <p>data.php</p> <pre><code>$fp = fopen('data.txt', 'w'); fwrite($fp, $argv[1]); fclose($fp); </code></pre> <p>A couple of notes:</p> <ul> <li><strong>It is important to pass each argument through</strong> <a href="http://php.net/manual/en/function.escapeshellarg.php" rel="nofollow"><code>escapeshellarg()</code></a> <strong>to ensure that users are not able inject commands into your shell</strong>. This will also stop special shell characters in arguments from breaking your scripts.</li> <li><code>$argv</code> is a <em>global</em> variable, not a <em>superglobal</em> like <code>$_GET</code> and <code>$_POST</code>. It is only available in the global scope. If you need to access it in a function scope, you can use <code>$GLOBALS['argv']</code>. This is about the only situation in which I consider the use of <code>$GLOBALS</code> acceptable, although it is still better to handle the arguments in the global scope on startup, and pass them through the scopes as arguments.</li> <li><code>$argv</code> is a 0-indexed array, but the first "argument" is in <code>$argv[1]</code>. <code>$argv[0]</code> always contains the path to the currently executing script, because <code>$argv</code> actually represents the arguments passed to the PHP binary, of which the path to your script is the first.</li> <li>Values from command line arguments always have a string type. PHP is very promiscuous with its typing so with scalar values this doesn't matter, but you (fairly obviously) can't pass vector types (objects, arrays, resources) through the command line. It is possible to pass objects and arrays by encoding them with e.g. <a href="http://php.net/serialize" rel="nofollow"><code>serialize()</code></a> or <a href="http://php.net/json-encode" rel="nofollow"><code>json_encode()</code></a>. There is no way to pass resources through the command line.</li> </ul> <p><strong>EDIT</strong> When passing vector types I prefer to use <code>serialize()</code> because it carries with it information about the classes that objects belong to.</p> <p>Here is an example:</p> <p>input.php:</p> <pre><code>$arg = array( 'I\'m', 'a', 'vector', 'type' ); exec("/usr/bin/php /path/to/data.php ".escapeshellarg(serialize($arg))." &gt; /dev/null 2&gt;&amp;1 &amp;"); </code></pre> <p>data.php</p> <pre><code>$arg = unserialize($argv[1]); $fp = fopen('data.txt', 'w'); foreach ($arg as $val) { fwrite($fp, "$val\n"); } fclose($fp); </code></pre> <p>Here is a couple of functions from my clip collection I use to simplify this process:</p> <pre><code>// In the parent script call this to start the child // This function returns the PID of the forked process as an integer function exec_php_async ($scriptPath, $args = array()) { $cmd = "php ".escapeshellarg($scriptPath); foreach ($args as $arg) { $cmd .= ' '.escapeshellarg(serialize($arg)); } $cmd .= ' &gt; /dev/null 2&gt;&amp;1 &amp; echo $$'; return (int) trim(exec($cmd)); } // At the top of the child script call this function to parse the arguments // Returns an array of parsed arguments converted to their correct types function parse_serialized_argv ($argv) { $temp = array($argv[0]); for ($i = 1; isset($argv[$i]); $i++) { $temp[$i] = unserialize($argv[$i]); } return $temp; } </code></pre> <p>If you need to pass a large amount of data (larger than the output of <code>getconf ARG_MAX</code> bytes) you should dump the serialized data to a file and pass the path to the file as a command line argument.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload