PHP code example of alexkart / curl-builder

1. Go to this page and download the library: Download alexkart/curl-builder library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

alexkart / curl-builder example snippets


$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

$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

$command->addOption('-L');
$command->addOption('-v');
$command->addOption('-H', 'Connection: keep-alive');
// curl -L -v -H 'Connection: keep-alive' ...

$command->addOption('-v');
$command->addOptions([
    '-L',
    '-d' => 'test'
]);
// curl -v -L -d 'test' ...

$command->setOptions(['-L', '-v']);
// curl -L -v ...

// 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'

$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

$command->addOption('-d', 'data');
// curl -d 'data'

$command->addOption('-d', "data'1");
// curl -d $'data\'1'

$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