PHP code example of amsify42 / php-curl-http

1. Go to this page and download the library: Download amsify42/php-curl-http 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/ */

    

amsify42 / php-curl-http example snippets


$curlHttp = new Amsify42\CurlHttp\CurlHttp('http://www.sample.com/users');

$curlHttp = get_curl_http('http://www.sample.com/users');

$curlHttp = new Amsify42\CurlHttp\CurlHttp('http://www.sample.com/user/create');
$curlHttp->setRequestMethod('POST');
$curlHttp->setRequestData(['name' => 'test', 'salary' => 123]);

$curlRequest = new Amsify42\CurlHtpp\Request();
$curlRequest->setMethod('POST');
/**
 * Set one of the header as Content-Type: application/json if you want to pass bodyData as json
 */
$curlRequest->setHeaders(['Content-Type: application/json']);
$curlRequest->setData(['name' => 'test', 'salary' => 123]);

$curlHttp = new Amsify42\CurlHttp\CurlHttp('http://www.sample.com/user/create', $curlRequest);

$curlRequest->setMethod('PUT');
$curlRequest->setHeaders(['Authorization: Bearer sf23rsdf23fds']);
/**
 * Set request ContentType as json if you want to pass bodyData as json
 */
$curlRequest->setContenType('json');
$curlRequest->setData(['name' => 'test', 'salary' => 123]);

$curlRequest->setData(json_encode(['name' => 'test', 'salary' => 123]));

$curlRequest->setHeaders(['Authorization' => 'Bearer sf23rsdf23fds']);

$curlHttp = get_curl_http('http://www.sample.com/users');
$response = $curlHttp->execute();

/* To get the response code */
$response->getCode();
/* To get the response body data */
$response->getBodyData();
/* To get the response headers as array */
$response->getHeaders();
/* To get the json decoded data of response */
$response->json();

$response = CurlHttp::get('https://www.somsite.com/users');
$response = CurlHttp::post('https://www.somsite.com/user/create', ['name' => 'dummy'], ['Authorization: Bearer sdf23rsdf23'], 'json');
$response = CurlHttp::put('https://www.somsite.com/user/2', ['name' => 'dummy...edited'], ['Authorization: Bearer sdf23rsdf23'], 'json');
$response = CurlHttp::delete('https://www.somsite.com/user/2', ['Authorization: Basic sdf23rsdf23']);

$response = CurlHttp::post('https://www.somsite.com/user/create', ['name' => 'dummy'], ['Authorization: Bearer sdf23rsdf23', 'Content-Type: application/json']);

$response = CurlHttp::post('https://www.somsite.com/user/create', json_encode(['name' => 'dummy']), ['Authorization: Bearer sdf23rsdf23']);