aracoool/dhttp

Simple and lightweight HTTP client based cURL

2.0.6 2020-12-10 15:23 UTC

This package is auto-updated.

Last update: 2024-11-10 23:18:00 UTC


README

dHttp is a lightweight library to work with PHP Curl. Easy-to-use library!

Latest Stable Version Build Status Scrutinizer Code Quality License

SensioLabsInsight

Install

  • Using packagist/composer: The recommended way to install library is through composer.
{
    "require": {
        "aracoool/dhttp" : "~2.0"
    }
}
  • Cloning the git repository

Requirements

dHttp (php curl library) works with PHP 7.3 7.4

Usage

GET request:

required __DIR__ . '/vendor/autoload.php';

// http://website.com?param1=value1
$client = new dHttp\Client(['http://website.com', [
    'param1' => 'value1'
]], [CURLOPT_TIMEOUT => 5]);

$resp = $client->get();
// Get response code
var_dump($resp->getCode());
// Get response body
var_dump($resp->getBody());
// Get request errors
var_dump($resp->getErrors());
// Return response headers
var_dump($resp->getHeaders());
// Return a specific (text/html; charset=utf-8)
var_dump($resp->getHeader('Content-Type'));

POST request:

required __DIR__ . '/vendor/autoload.php';

$client = new dHttp\Client('http://website.com');
$client->addOptions([CURLOPT_RETURNTRANSFER => false])
	->setCookie('/tmp/cookie.txt')
	->setUserAgent('Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31')
	->post(array(
		'field1' => 'value1',
		'field2' => 'value2',
));

Multithreaded query:

required __DIR__ . '/vendor/autoload.php';

$client = new dHttp\Client();
$response_array = $client->multi(array(
	new dHttp\Client('http://website1.com'),
	new dHttp\Client('http://website2.com', array(
		CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 5.1; rv:5.0.1) Gecko/20100101 Firefox/5.0.1',
		CURLOPT_TIMEOUT => 5,
	))
));

foreach($response_array as $item) {
	var_dump($item->getCode());
}

Get cURL version:

\dHttp\Client::v();