GithubHelp home page GithubHelp logo

curl-builder's Introduction

curl-builder build Code Coverage

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

curl-builder's People

Contributors

alexkart avatar peter279k avatar xepozz avatar samdark avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.