otifsolutions/curl-handler

This package provides a eaisy access to api data in the project by simply giving the method,url and parameters

V1.0.5 2023-03-08 13:19 UTC

This package is auto-updated.

Last update: 2024-05-08 15:40:30 UTC


README

An Easy to use Curl class. Allows single-line easy API calls.

Requirements

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\Curl

Methods used with the package's curl class

url('')

header([])

params([])

body([])

execute()

getCurlErrors();    // used to display errors if any

Supported 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'])
        ->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`,
`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

laravel-currency-layer/Commands/FetchCurrencyRates