当前位置:网站首页>[mikehaertl/php-shellcommand]一个用于调用外部命令操作的库
[mikehaertl/php-shellcommand]一个用于调用外部命令操作的库
2022-08-02 03:24:00 【phpreturn】
它提供了一个简单地方法接口来调用操作外部命令,可以用来替换exec.
相比直接调用exec,它具有以下几个特点:
- 捕捉, stdOut,stdErr,exitCode
- 处理复杂的参数
- 支持环境变量和其他参数
- 支持管道资源,比如文件或流
- 可以超时执行
基本使用
<?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();
}
高级使用
添加参数
<?php
$command = new Command('/bin/somecommand');
// 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'));
字符串输入
<?php
$command = new ('jq') // jq is a pretty printer
$command->setStdIn('{"foo": 0}');
if (!$command->execute()) {
echo $command->getError();
} else {
echo $command->getOutput();
}
// Output:
// {
// "foo": 0
// }
把文件内容当做输入
<?php
$fh = fopen('test.json', 'r');
// error checks left out...
$command = new Command('jq');
$command->setStdIn($fh);
if (!$command->execute()) {
echo $command->getError();
} else {
echo $command->getOutput();
}
fclose($fh);
把URL内容当做输入
<?php
$fh = fopen('https://api.open-meteo.com/v1/forecast?latitude=52.52&longitude=13.41&hourly=temperature_2m,relativehumidity_2m,windspeed_10m', 'r');
// error checks left out...
$command = new Command('jq');
$command->setStdIn($fh);
if (!$command->execute()) {
echo $command->getError();
} else {
echo $command->getOutput();
}
fclose($fh);
将命令封装好
<?php
// 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,
),
));
支持更多的参数方法
支持我们调用外部命令书需要多少所有方法,比如获取输出,获取错误,获取退出代码等.
边栏推荐
猜你喜欢
解决MySQL创建子视图并查看的时候,字符集报错问题
利用 nucleo stm32 f767zi 进行USART+DMA+PWM输入模式 CUBE配置
Circular linked list---------Joseph problem
canvas--pie chart
canvas--饼状图
【 application 】 life many years of operations, what turned scored 12 k + annual bonus salary?
ES6数组的扩展方法map、filter、reduce、fill和数组遍历for…in for…of arr.forEach
阿里云服务器如何使用admin账户登录
Small program van-cell line wrapping can be left-aligned
npm --package.json---require
随机推荐
阿里云服务器如何使用admin账户登录
C语言 void和void *(无类型指针)
meime模块
DSPE-PEG-PDP, DSPE-PEG-OPSS, phospholipid-polyethylene glycol-mercaptopyridine supply, MW: 5000
URL module
1.uview form校验位置可以改变 2.时间区间
骨架效果 之高级渐变,适用图片等待时
三月底啦啦
STM32 map文件解析
Relative and absolute paths
【手把手带你学nRF52832/nRF52840 · (1)开发环境搭建】
canvas--饼状图
我的小笔记 =》原生微信小程序
clock tick marks
DOM操作---放大镜案例
Source Insight 使用教程(2)——常用功能
点名系统和数组元素为对象的排序求最大值和最小值
微信小程序云开发如何将页面生成为pdf?
v-on基本使用、参数传递、修饰词
ES6介绍+定义变量+不同情况下箭头函数的this指向