{"id":91,"date":"2011-04-13T19:49:10","date_gmt":"2011-04-13T16:49:10","guid":{"rendered":"http:\/\/meekconsulting.com\/?p=91"},"modified":"2011-04-13T19:49:52","modified_gmt":"2011-04-13T16:49:52","slug":"run-subcommand-and-return-output-to-variable-in-php","status":"publish","type":"post","link":"https:\/\/meekconsulting.com\/?p=91","title":{"rendered":"Run subcommand and return output to variable in PHP"},"content":{"rendered":"<p>If you need to run a subcommand and pull the output you can use the proc_open like below.<\/p>\n<p>Kevin<\/p>\n<blockquote><p>&lt;?php<\/p>\n<p>&nbsp;<\/p>\n<p>$descriptorspec = array(<\/p>\n<p>0 =&gt; array(&#8220;pipe&#8221;, &#8220;r&#8221;),\u00a0 \/\/ stdin is a pipe that the child will read from<\/p>\n<p>1 =&gt; array(&#8220;pipe&#8221;, &#8220;w&#8221;),\u00a0 \/\/ stdout is a pipe that the child will write to<\/p>\n<p>2 =&gt; array(&#8220;file&#8221;, &#8220;\/tmp\/yourprocoutput.txt&#8221;, &#8220;a&#8221;) \/\/ stderr is a file to write to<\/p>\n<p>);<\/p>\n<p>$process = proc_open(&#8220;\/bin\/ls -l \/tmp&#8221;, $descriptorspec, $pipes);<\/p>\n<p>while (!feof($pipes[1])) {<\/p>\n<p>$result .= fread($pipes[1], 8192);<\/p>\n<p>print &#8220;.&#8221;;<\/p>\n<p>}<\/p>\n<p>print &#8220;RESULT: [$result] \\n&#8221;;<\/p>\n<p>?&gt;&nbsp;<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>If you need to run a subcommand and pull the output you can use the proc_open like below. Kevin &lt;?php &nbsp; $descriptorspec = array( 0 =&gt; array(&#8220;pipe&#8221;, &#8220;r&#8221;),\u00a0 \/\/ stdin is a pipe that the child will read from 1 &hellip; <a href=\"https:\/\/meekconsulting.com\/?p=91\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[7],"class_list":["post-91","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-php"],"_links":{"self":[{"href":"https:\/\/meekconsulting.com\/index.php?rest_route=\/wp\/v2\/posts\/91","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meekconsulting.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meekconsulting.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meekconsulting.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/meekconsulting.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=91"}],"version-history":[{"count":2,"href":"https:\/\/meekconsulting.com\/index.php?rest_route=\/wp\/v2\/posts\/91\/revisions"}],"predecessor-version":[{"id":93,"href":"https:\/\/meekconsulting.com\/index.php?rest_route=\/wp\/v2\/posts\/91\/revisions\/93"}],"wp:attachment":[{"href":"https:\/\/meekconsulting.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=91"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meekconsulting.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=91"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meekconsulting.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=91"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}