PHP code example of icemont / curlwrapper

1. Go to this page and download the library: Download icemont/curlwrapper 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/ */

    

icemont / curlwrapper example snippets


use Icemont\cURL\CurlWrapper;

$curl = new CurlWrapper();

/*
 * Preparing request
 */

$curl->setTimeout(5)
     ->setUserAgent('Mozilla/5.0 (compatible; CurlWrapper/2.0)')
     ->setReferer('https://example.com/')
     ->addHeader('API-Key: TEST_KEY')
     ->addData('test', 'value')
     ->addData('param2', 'value2')
     ->addDataFromArray([
        'fromArray1' => 'valueA',
        'fromArray2' => 'valueB',
    ]);

/**
 * Executing the query
 */
var_dump($curl->postRequest('https://httpbin.org/post'));

echo 'Request response code: ' . $curl->getLastCode() . PHP_EOL;
echo 'Request error string: ' . $curl->getLastError() . PHP_EOL;


use Icemont\cURL\CurlWrapper;

$curl = new CurlWrapper();

// Executing an empty POST request
var_dump($curl->postRequest('https://httpbin.org/post'));

// Add data and execute a POST request with data sending
$curl->addData('value', 'test')
     ->addData('value2', 'test2');
var_dump($curl->postRequest('https://httpbin.org/post'));


// Execute a simple GET request (data will be added as Query String)
var_dump($curl->getRequest('https://httpbin.org/get'));

// Reset the data and execute a simple GET request again
$curl->reset();
var_dump($curl->getRequest('https://httpbin.org/get'));


use Icemont\cURL\CurlWrapper;

$api = new CurlWrapper();

/**
 * Adding data one by one
 */
$api->addHeader('X-API-Key: YOUR_API_KEY')
    ->addData('source', 'API')
    ->addData('name', 'Test User')
    ->addData('email', '[email protected]')
    ->addData('ip', '127.0.0.1');

/**
 * Or immediately in the array
 */
$api->addDataFromArray([
    'alert' => true,
    'autorespond' => true,
    'subject' => 'Testing API',
    'message' => 'MESSAGE HERE',
]);

/**
 * Executing the query as JSON
 */
var_dump($api->jsonRequest('https://support.example.com/api/tickets.json'));

echo 'Request response code: ' . $api->getLastCode() . PHP_EOL;
echo 'Request error string: ' . $api->getLastError() . PHP_EOL;