An Easy to use Curl class. Allows single-line easy API calls.
PHP 7 > PHP 8.1
How to use the Library
Install via Composer Composer (Recommended)
Using Composer (Recommended)
composer require otifsolutions/curl-handler
Namespace for the package class
use OTIFSolutions\CurlHandler\CurlMethods used with the package's curl class
url('')
header([])
params([])
body([])
referer('')
agent('')
execute()
getCurlErrors(); // used to display errors if anySupported Request Methods:
GET
POST
PUT
DELETE
How to use the package:
use OTIFSolutions\CurlHandler\Curl;
use OTIFSolutions\CurlHandler\Exceptions\CurlException;
try{
Curl::Make()
->GET // this could be, get, post, put, delete
->url('REQUEST_URL_GOES_HERE')
->header(['AUTHENTICATION_ARRAY_GOES_HERE'])
->body(['BODY_ARRAY_GOES_HERE'])
->referer('https://www.google.com')
->agent('Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0.1) Gecko/20100101 Firefox/7.0.12011-10-16 20:23:00')
->params(['PARAMETERS_ARRAY_GOES_HERE'])
->execute();
}
catch(CurlException $ce){
return ($ce->getCurlErrors());
}Method signatures of all the methods/requests used in the package
`url('STRING') : Object`,
`header(['ARRAY']) : Object`,
`body(['ARRAY']) : Object`,
`params(['ARRAY]) : Object`,
`referer('STRING') : Object`,
`agent('STRING') : Object`,
`execute() : array`,
`getCurlErrors() : array`,
`isJson('string'): bool`,
`isDomDocument('string'): bool`,
`domToArray($node): mixed`
If you are using PhpStorm IDE then you don't have to check method signatures every time,
just go to the method, click it, then do CTRL + Q on it, everything that belongs to this method, will be shown.
Get request for API call
use OTIFSolutions\CurlHandler\Curl;
Curl::Make()
->GET
->url('URL_GOES_HERE')
->header(['AUTHENTICATION_ARRAY_GOES_HERE'])
->params(['PARAMS_ARRAY_GOES_HERE'])
->execute();Post request
use OTIFSolutions\CurlHandler\Curl;
Curl::Make()
->POST
->url('URL_GOES_HERE')
->header(['AUTHENTICATION_ARRAY_GOES_HERE'])
->body(['BODY_ARRAY_GOES_HERE'])
->params(['PARAMS_ARRAY_GOES_HERE'])
->execute();Put Request
use OTIFSolutions\CurlHandler\Curl;
Curl::Make()
->PUT
->url('URL_GOES_HERE')
->header(['AUTHENTICATION_ARRAY_GOES_HERE'])
->body(['BODY_ARRAY_GOES_HERE'])
->params(['PARAMS_ARRAY_GOES_HERE'])
->execute();Delete request
use OTIFSolutions\CurlHandler\Curl;
Curl::Make()
->DELETE
->url('URL_GOES_HERE')
->header(['AUTHENTICATION_ARRAY_GOES_HERE'])
->params(['PARAMS_ARRAY_GOES_HERE'])
->execute();Note (Precaution):
If you call any method that does not belong to the OTIFSolutions\CurlHandler\Curl::class or give any parameter that it does not understand, then you will see the error messages.
Realtime example you can check
This example demonstrates the usage of curl-handleer with get method, have a look at