PHP Curl Class
This library provides an object-oriented wrapper of the PHP cURL extension., (*1)
If you have questions or problems with installation or usage create an Issue., (*2)
Installation
In order to install this library via composer run the following command in the console:, (*3)
composer require firmaprofesional/curl
or add the package manually to your composer.json file in the require section:, (*4)
"firmaprofesional/curl": "^0.3"
Usage examples
$curl = new CurlService();
$curlConfig = new CurlConfig();
$curlConfig->setCurlUrl('www.example.com');
$curl->configure($curlConfig);
$curl->send();
basic authentication, (*5)
$curl = new CurlService();
$curlConfig = new CurlConfig();
$curlConfig->setCurlUrl('www.example.com');
$curlConfig->setUsername('user');
$curlConfig->setUserPassword('password');
$curl->configure($curlConfig);
$curl->send();
post fields, (*6)
$curl = new CurlService();
$curlConfig = new CurlConfig();
$curlConfig->setCurlUrl('www.example.com');
$curlConfig->setMethodPOST();
$data_string = json_encode(array('data'));
$curlConfig->setHttpHeader(
array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string),
)
);
$curlConfig->setData($data_string);
$curl->configure($curlConfig);
$curl->send();
post fields with bearer token authentication, (*7)
$curl = new CurlService();
$curlConfig = new CurlConfig();
$curlConfig->setCurlUrl('www.example.com');
$curlConfig->setMethodPOST();
$data_string = json_encode(array('data'));
$curlConfig->setHttpHeader(
array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string),
'Authorization: Bearer bearertoke'
)
);
$curlConfig->setData($data_string);
$curl->configure($curlConfig);
$curl->send();
enable verbose mode will log on tmp path, (*8)
$curl = new CurlService();
$curlConfig = new CurlConfig();
$curlConfig->setCurlUrl('www.example.com');
$curlConfig->setVerbose(true);
$curl->configure($curlConfig);
$curl->send();
set timeout in seconds, (*9)
$curl = new CurlService();
$curlConfig = new CurlConfig();
$curlConfig->setCurlUrl('www.example.com');
$curlConfig->setTimeout(10);
$curl->configure($curlConfig);
$curl->send();
Testing
In order to test the library:, (*10)
- Create a fork
- Clone the fork to your machine
- Install the depencies
composer install
- Run the unit tests
./vendor/phpunit/bin/phpunit -c phpunit.xml --testsuite general