2017 © Pedro Peláez
 

library curl

An easy to use OOP wrapper for cURL

image

alisaleem/curl

An easy to use OOP wrapper for cURL

  • Thursday, February 15, 2018
  • by alisaleem
  • Repository
  • 0 Watchers
  • 0 Stars
  • 99 Installations
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 7 Versions
  • 1 % Grown

The README.md

cURL

An easy to use OOP wrapper for cURL, (*1)

Set Up

  • Add package to your project using composer, (*2)

    composer require alisalemm/curl:dev-master, (*3)

  • Include autoloader in your PHP file, (*4)

    require 'vendor/autoload.php';, (*5)

  • Select correct namespace for the classes in your PHP file, (*6)

use \AliSaleem\Curl\Request;
use \AliSaleem\Curl\Response;

Examples

GET Request

$request = new Request('https://requestb.in/xxxxxxxx');
$response = $request->get();

echo $response->getBody();

GET Request with Parameters

$response = $request
    ->setParameters([
        'paramA' => 'valueA',    
        'paramB' => 'valueB',    
    ])
    ->addParameter('paramC', 'valueC')
    ->get();

POST Request

$response = $request
    ->setBody('body content')
    ->post();

The body content can be of the following types, (*7)

  • String
  • Array
  • Any object that implements JsonSerializable

Other Request Methods

$response = $request->put();
$response = $request->delete();
$response = $request->options();
$response = $request->head();

Custom Request Method

$response = $request->request('CUSTOM_METHOD');

Add Headers

$response = $request
    ->setHeaders([
        'headerA' => 'valueA',    
        'headerB' => 'valueB',    
    ])
    ->addHeader('headerC', 'valueC')
    ->get();

Customising Wrapper Options

$response = $request
    ->setOptions([
        'optionA' => 'valueA',    
        'optionB' => 'valueB',    
    ])
    ->setOption('optionC', 'valueC')
    ->get();

The following options can be set, (*8)

Option Description Default Value
return_body CURLOPT_RETURNTRANSFER true
follow_redirects CURLOPT_FOLLOWLOCATION true
header CURLOPT_HEADER true
return_header CURLINFO_HEADER_OUT true
timeout CURLOPT_TIMEOUT 30
verify_peer CURLOPT_SSL_VERIFYPEER false
delay delay (seconds) between retry attempts 0
tries max request attempts if validation fails 1
multiple_headers allow multiple headers false

Adding a validation method for responses

Retries are attempted if the validation fails, (*9)

use \AliSaleem\Curl\Request;
use \AliSaleem\Curl\Response;

class MyClass {

    public function myValidationMethod(Response $response)
    {
        // $response->getHttpCode();
        // $response->getHeaders();
        // $response->getHeader('headerName');
        // $response->getBody();
        // $response->getInfo();
        // $response->getError();
        // $response->getErrorNo();

        // return true | false;
    }

    public function makeCurlRequest()
    {
        $request = new Request('https://requestb.in/xxxxxxxx');

        $response = $request
            ->setOption('tries', 3)
            ->setValidationMethod($this, 'myValidationMethod')
            ->get();

        echo $response->getBody();
    }
}

The Versions

15/02 2018

dev-master

9999999-dev https://bitbucket.org/AliSaleem/curl

An easy to use OOP wrapper for cURL

  Sources   Download

15/02 2018

1.1.3

1.1.3.0 https://bitbucket.org/AliSaleem/curl

An easy to use OOP wrapper for cURL

  Sources   Download

09/02 2018

1.1.2

1.1.2.0 https://bitbucket.org/AliSaleem/curl

An easy to use OOP wrapper for cURL

  Sources   Download

09/02 2018

1.1.1

1.1.1.0 https://bitbucket.org/AliSaleem/curl

An easy to use OOP wrapper for cURL

  Sources   Download

19/01 2018

1.1.0

1.1.0.0 https://bitbucket.org/AliSaleem/curl

An easy to use OOP wrapper for cURL

  Sources   Download

12/01 2018

1.0.1

1.0.1.0 https://bitbucket.org/AliSaleem/curl

An easy to use OOP wrapper for cURL

  Sources   Download

09/01 2018

1.0.0

1.0.0.0 https://bitbucket.org/AliSaleem/curl

An easy to use OOP wrapper for cURL

  Sources   Download