alexkart / curl-builder
PSR-7 compatible curl builder.
Installs: 292 472
Dependents: 2
Suggesters: 0
Security: 0
Stars: 19
Watchers: 2
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
- dev-master
- 1.1.0
- 1.0.9
- 1.0.8
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- dev-codex/add-php-8.4-to-github-actions-config
- dev-codex/review-tests-and-add-missing-cases
- dev-codex/add-test-for-curl-command-options
- dev-codex/update-readme-constants-names
- dev-codex/update-comment-in-commandtest.php
- dev-codex/find-and-fix-a-bug-in-codebase
This package is auto-updated.
Last update: 2025-06-05 09:43:45 UTC
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