Github :https://github.com/mikehaertl/php-shellcommand

安装 (php > 5.4)

composer require mikehaertl/php-shellcommand

例子

<?php
use mikehaertl\shellcommand\Command;

// Basic example
$command = new Command('/usr/local/bin/mycommand -a -b');
if ($command->execute()) {
    echo $command->getOutput();
} else {
    echo $command->getError();
    $exitCode = $command->getExitCode();
}

高级功能

// Create command with options array
$command = new Command(array(
    'command' => '/usr/local/bin/mycommand',

    // Will be passed as environment variables to the command
    'procEnv' => array(
        'DEMOVAR' => 'demovalue'
    ),

    // Will be passed as options to proc_open()
    'procOptions' => array(
        'bypass_shell' => true,
    ),
));

// Add arguments with correct escaping:
// results in --name='d'\''Artagnan'
$command->addArg('--name=', "d'Artagnan");

// Add argument with several values
// results in --keys key1 key2
$command->addArg('--keys', array('key1','key2'));

// Add string to pipe to command on standard input
$command->setStdIn('string');

API

1. $escapeArgs: Whether to escape any argument passed through addArg(). Default is true.

2. $escapeCommand: Whether to escape the command passed to setCommand() or the constructor. This is only useful if $escapeArgs is false. Default is false.

3. $useExec: Whether to use exec() instead of proc_open(). This is a workaround for OS which have problems with proc_open(). Default is false.

4. $captureStdErr: Whether to capture stderr when useExec is set. This will try to redirect the otherwhise unavailable stderr to stdout, so that both have the same content on error. Default is true.

5. $procCwd: The initial working dir passed to proc_open(). Default is null for current PHP working dir.

6. $procEnv: An array with environment variables to pass to proc_open(). Default is null for none.

7. $procOptions: An array of other_options for proc_open(). Default is null for none.

8. $locale: The locale to (temporarily) set with setlocale() before running the command. This can be set to e.g. en_US.UTF-8 if you have issues with UTF-8 encoded arguments.

方法

Properties
$escapeArgs: Whether to escape any argument passed through addArg(). Default is true.
$escapeCommand: Whether to escape the command passed to setCommand() or the constructor. This is only useful if $escapeArgs is false. Default is false.
$useExec: Whether to use exec() instead of proc_open(). This is a workaround for OS which have problems with proc_open(). Default is false.
$captureStdErr: Whether to capture stderr when useExec is set. This will try to redirect the otherwhise unavailable stderr to stdout, so that both have the same content on error. Default is true.
$procCwd: The initial working dir passed to proc_open(). Default is null for current PHP working dir.
$procEnv: An array with environment variables to pass to proc_open(). Default is null for none.
$procOptions: An array of other_options for proc_open(). Default is null for none.
$locale: The locale to (temporarily) set with setlocale() before running the command. This can be set to e.g. en_US.UTF-8 if you have issues with UTF-8 encoded arguments.
You can configure all these properties via an array that you pass in the constructor. You can also pass command, execCommand and args as options. This will call the respective setter (setCommand(), setExecCommand(), etc.).

Methods
__construct($options = null)
    $options: either a command string or an options array (see setOptions())

setOptions($options): Set command options
    $options: array of name => value options that should be applied to the object. You can also pass options that use a setter, e.g. you can pass a command option which will be passed to setCommand().

setCommand($command): Set command
    $command: The command or full command string to execute, like gzip or gzip -d. You can still call addArg() to add more arguments to the command. If $escapeCommand was set to true, the command gets escaped through escapeshellcmd().

getCommand(): The command that was set through setCommand() or passed to the constructor.

getExecCommand(): The full command string to execute.

setArgs($args): Set argument as string
    $args: The command arguments as string. Note, that these will not get escaped!

getArgs(): The command arguments that where set through setArgs() or addArg(), as string

addArg($key, $value=null, $escape=null): Add argument with correct escaping
    $key: The argument key to add e.g. --feature or --name=. If the key does not end with and =, the $value will be separated by a space, if any. Keys are not escaped unless $value is null and $escape is true.
    $value: The optional argument value which will get escaped if $escapeArgs is true. An array can be passed to add more than one value for a key, e.g. addArg('--exclude', array('val1','val2')) which will create the option "--exclude 'val1' 'val2'".
    $escape: If set, this overrides the $escapeArgs setting and enforces escaping/no escaping

setStdIn(): String or resource to supply to command via standard input.

getOutput(): The command output as string. Empty if none.

getError(): The error message, either stderr or internal message. Empty if no error.

getStdErr(): The stderr output. Empty if none.

getExitCode(): The exit code.

getExecuted(): Whether the command was successfully executed.

execute(): Executes the command and returns true on success, false otherwhise.

解决 laravel-admin between datetime 假如数据库是时间戳int类型无法筛选。

laravel-admin默认的between->datetime(),查询默认是datetime类型,但是假如数据库是时间戳类型就会报错,又不想改底层文件的话可以试试加自定义筛选功能...

阅读全文

php解析英文语句,自动分解。

参考:https://www.php.net/manual/en/function.str-split.php 最近碰到一个问题,客户的英文地址太长,超出接口api字段长度,所以需要解析下语句分解发送。 ...

阅读全文

记录一个laravel-excel导出表格值为0导出excel显示空的解决方法。

最近在使用laravel-excel导出表格的时候,发现假如字段值为0的情况下,导出的excel中直接显示为空,找到一个方法解决,如下. 在laravel-excel的config配置中...

阅读全文

1 条评论

回复 文娱帝国 取消回复