alexkart/curl-builder
最新稳定版本:1.1.0
Composer 安装命令:
composer require alexkart/curl-builder
包简介
PSR-7 compatible curl builder.
README 文档
README
curl-builder is a curl command generator which can generate curl commands automatically from PSR-7 server requests and manually by specifying options and URL.
Installation
composer require alexkart/curl-builder
Examples
Generating curl command from PSR-7 request
$request = new Request('POST', 'http://example.com', [ 'Connection' => ['keep-alive'], 'Accept' => [ 'text/html', 'application/xhtml+xml', ], ], 'data'); $command = new Command(); $command->setRequest($request); $curl = $command->build(); // curl -H 'Connection: keep-alive' -H 'Accept: text/html, application/xhtml+xml' -d 'data' http://example.com
Constructing curl command manually
$command = new Command(); $command->setUrl('http://example.com'); $command->addOption('-v'); $command->addOption('-H', 'Connection: keep-alive'); $command->addOption('-H', 'Cache-Control: max-age=0'); $curl = $command->build(); // curl -v -H 'Connection: keep-alive' -H 'Cache-Control: max-age=0' http://example.com
Adding options
Options can be added to the command one by one with addOption()
$command->addOption('-L'); $command->addOption('-v'); $command->addOption('-H', 'Connection: keep-alive'); // curl -L -v -H 'Connection: keep-alive' ...
or add several of them at once with addOptions()
$command->addOption('-v'); $command->addOptions([ '-L', '-d' => 'test' ]); // curl -v -L -d 'test' ...
setOptions() can be used to override previously set options
$command->setOptions(['-L', '-v']); // curl -L -v ...
addOptions() and setOptions() formats:
// options without arguments // the following lines will generate the same command $command->setOptions(['-L' => [null], '-v' => [null]]); $command->setOptions(['-L' => null, '-v' => null]); $command->setOptions(['-L', '-v']); // curl -L -v ... // options with arguments $command->setOptions(['-H' => 'test']); // curl -H 'test' ... $command->setOptions(['-H' => ['test1', 'test2']]); // curl -H 'test1' -H 'test2' ...
Specifying command template
Default template for the command is {name}{options}{url}. But you can change it with setTemplate() method
$command = new Command(); $command->setUrl('http://example.com'); $command->addOption('-v'); $command->addOption('-L'); $curl = $command->build(); // curl -v -L http://example.com // change order $command->setTemplate(Command::TEMPLATE_COMMAND_NAME . Command::TEMPLATE_URL . Command::TEMPLATE_OPTIONS); $curl = $command->build(); // curl http://example.com -v -L // remove options $command->setTemplate(Command::TEMPLATE_COMMAND_NAME . Command::TEMPLATE_URL); $curl = $command->build(); // curl http://example.com
Quoting and escaping arguments
By default arguments are quoted with single quotes and if single quote appears in the argument it will be escaped
$command->addOption('-d', 'data'); // curl -d 'data' $command->addOption('-d', "data'1"); // curl -d $'data\'1'
Quoting character can be changed to double quote or removed
$command->addOption('-d', 'data1'); $command->addOption('-d', 'data"2'); $command->setQuoteCharacter(Command::QUOTE_DOUBLE); // curl -d "data1" -d "data\"2" $command->addOption('-d', 'data'); $command->setQuoteCharacter(Command::QUOTE_NONE); // curl -d data $command->addOption('-d', 'value with spaces'); $command->setQuoteCharacter(Command::QUOTE_NONE); // curl -d value\ with\ spaces
统计信息
- 总下载量: 366.41k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 19
- 点击次数: 1
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-07-23