最近项目需求通过curl执行远程服务器脚本,脚本都写好之后,发现在远程服务器命令行中可以正常执行,但是通过curl模拟浏览器访问或者直接浏览器访问时脚本无法正常运行,谷歌百度之后找到一个排错方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php function my_exec($cmd, $input=''){ $proc=proc_open($cmd,array( 0=>array('pipe','r'), 1=>array('pipe','w'), 2=>array('pipe','w'), ),$pipes); fwrite($pipes[0], $input);fclose($pipes[0]); $stdout=stream_get_contents($pipes[1]);fclose($pipes[1]); $stderr=stream_get_contents($pipes[2]);fclose($pipes[2]); $rtn=proc_close($proc); return array( 'stdout'=>$stdout, 'stderr'=>$stderr, 'return'=>$rtn, ); } $str = "echo 11>/tmp/suvan.log"; //此处为我要检测是否执行成功的指令"echo 11>/tmp/suvan.log" var_export(my_exec($str)); ?> |