alexkart / curl-builder
PSR-7 compatible curl builder.
Installs: 218 174
Dependents: 2
Suggesters: 0
Security: 0
Stars: 19
Watchers: 3
Forks: 4
Open Issues: 0
Requires
- php: >=7.3
- psr/http-message: ^2.0
Requires (Dev)
- nyholm/psr7: ^1.3
- phan/phan: ^5.4
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^9.5
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 -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}{option}{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_CHARACTER_DOUBLE); // curl -d "data" -d "data\"2" $command->addOption('-d', 'data'); $command->setQuoteCharacter(Command::QUOTE_CHARACTER_NONE); // curl -d data