Skip to content

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

License

Notifications You must be signed in to change notification settings

otifsolutions/curl-handler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Curl-Handler

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

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages