{"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><?php<\/p>\n<p>\u00a0<\/p>\n<p>$descriptorspec = array(<\/p>\n<p>0 => array(\u201cpipe\u201d, \u201cr\u201d),\u00a0 \/\/ stdin is a pipe that the child will read from<\/p>\n<p>1 => array(\u201cpipe\u201d, \u201cw\u201d),\u00a0 \/\/ stdout is a pipe that the child will write to<\/p>\n<p>2 => array(\u201cfile\u201d, \u201c\/tmp\/yourprocoutput.txt\u201d, \u201ca\u201d) \/\/ stderr is a file to write to<\/p>\n<p>);<\/p>\n<p>$process = proc_open(\u201c\/bin\/ls -l \/tmp\u201d, $descriptorspec, $pipes);<\/p>\n<p>while (!feof($pipes[1])) {<\/p>\n<p>$result .= fread($pipes[1], 8192);<\/p>\n<p>print \u201c.\u201d;<\/p>\n<p>}<\/p>\n<p>print \u201cRESULT: [$result] \\n\u201d;<\/p>\n<p>?>\u00a0<\/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 \u00a0<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[7],"_links":{"self":[{"href":"https:\/\/meekconsulting.com\/index.php?rest_route=\/wp\/v2\/posts\/91"}],"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}]}}