2017 © Pedro Peláez
 

library php-curl

Lightweight PHP cURL wrapper

image

sylouuu/php-curl

Lightweight PHP cURL wrapper

  • Friday, September 23, 2016
  • by sylouuu
  • Repository
  • 4 Watchers
  • 25 Stars
  • 1,651 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 12 Forks
  • 0 Open issues
  • 14 Versions
  • 5 % Grown

The README.md

PHP cURL

Build Status Version CodeClimate, (*1)

Requirements

  • PHP >= 5.4
  • cURL library enabled

Install

Composer

{
    "require": {
        "sylouuu/php-curl": "0.8.*"
    }
}
require_once './vendor/autoload.php';

Usage

// Namespace shortcut
use sylouuu\Curl\Method as Curl;

// Template
$request = new Curl\<METHOD_NAME>( string $url [, array $options ] );

Methods are:, (*2)

  • Get()
  • Head()
  • Options()
  • Post()
  • Put()
  • Patch()
  • Delete()

Constructor $options

[
    'data' => [             // Data to send, available for `Post`, `Put` and `Patch`
        'foo' => 'bar'
    ],
    'headers' => [          // Additional headers (optional)
        'Authorization: foobar'
    ],
    'ssl' => '/cacert.pem', // Use it for SSL (optional)
    'is_payload' => true,   // `true` for sending a payload (JSON-encoded data, optional)
    'autoclose' => true     // Is the request must be automatically closed (optional)
]

Public methods

// Send a request
$request->send();

// HTTP status code
$request->getStatus();

// HTTP header
$request->getHeader();

// HTTP body response
$request->getResponse();

// Used cURL options
$request->getCurlOptions();

// Set a cURL option
$request->setCurlOption(CURLOPT_SOMETHING, $value);

// Manually close the handle (necessary when `autoclose => false` is used)
$request->close();

Examples

Basic:, (*3)

// Namespace shortcut
use sylouuu\Curl\Method as Curl;

// Standard GET request
$request = new Curl\Get('http://domain.com');

// Send this request
$request->send();

echo $request->getResponse(); // body response
echo $request->getStatus(); // HTTP status code

Send a payload:, (*4)

// Namespace shortcut
use sylouuu\Curl\Method as Curl;

// JSON-encoded POST request
$request = new Curl\Post($this->endpoint, [
    'data' => [
        'name' => 'foo',
        'email' => 'foo@domain.com'
    ],
    // With 'is_payload' => true
    // You don't have to json_encode() your array of data
    // Moreover, the appropriate headers will be set for you
    'is_payload' => true
]);

// Send this request
$request->send();

echo $request->getResponse(); // body response
echo $request->getStatus(); // HTTP status code

Manual closing:, (*5)

// Namespace shortcut
use sylouuu\Curl\Method as Curl;

// Set `autoclose` option to `false`
$request = new Curl\Get('http://domain.com', [
    'autoclose' => false
]);

// Send this request
$request->send();

// Now you can retrieve a cURL info as the handle is still open
$request->getCurlInfo(CURLINFO_SOMETHING);

echo $request->getResponse();

// Manually close the handle
$request->close();

Tests

On project directory:, (*6)

  • composer install to retrieve phpunit
  • phpunit to run tests

Changelog

2015-08-25 - 0.8.1, (*7)

  • fixed invalid content-length in case of no data provided

2015-07-03 - 0.8.0, (*8)

  • added is_payload option to perform a request with JSON-encoded data
  • fixed ssl option

2014-10-23 - 0.7.1, (*9)

  • fixed Post() which didn't send data in some cases

2014-08-01 - 0.7.0 (BC break), (*10)

  • moved from psr-0 autoload to psr-4
  • added Method directory, then methods are now in \sylouuu\Curl\Method\

2014-05-30 - 0.6.1, (*11)

  • removed exception when data option is not specified for Post Put and Patch

2014-05-22 - 0.6.0 (BC break), (*12)

  • moved url option to the first constructor parameter

2014-05-20 - 0.5.0 (BC break), (*13)

  • renamed repository from php-rest-client to php-curl
  • refactored all code
  • added autoclose option
  • added the way to get/set cURL options
  • added the way to get cURL info
  • sources are now psr-2 compliant

2014-05-13 - 0.4.0 (BC break), (*14)

  • renamed RESTClient.class.php to RESTClient.php
  • moved RESTClient.php to /src
  • moved RESTClientTest.php to /tests
  • added HEAD, OPTIONS and PATCH support
  • added getHeader method
  • renamed getJSON to getResponse
  • removed JSON validation
  • added sylouuu namespace
  • removed gulp

2014-05-09 - 0.3.0, (*15)

  • added ssl option

2014-04-06 - 0.2.1, (*16)

  • added exception if invalid JSON format returned

2014-04-04 - 0.2.0, (*17)

  • new way to retrieve result
  • added HTTP status code

2014-04-01 - 0.1.0, (*18)

  • refactored class
  • removed constructor parameter
  • added unit tests

2014-03-24 - 0.0.2, (*19)

  • added $api_url as a constructor parameter

2014-02-05 - 0.0.1, (*20)

  • Initial release

The Versions

23/09 2016

dev-master

9999999-dev https://github.com/sylouuu/php-curl

Lightweight PHP cURL wrapper

  Sources   Download

MIT

The Requires

  • php >5.3.0

 

The Development Requires

curl http

25/08 2015

0.8.1

0.8.1.0 https://github.com/sylouuu/php-curl

Lightweight PHP cURL wrapper

  Sources   Download

MIT

The Requires

  • php >5.3.0

 

The Development Requires

curl http

03/07 2015

0.8.0

0.8.0.0 https://github.com/sylouuu/php-curl

Lightweight PHP cURL wrapper

  Sources   Download

MIT

The Requires

  • php >5.3.0

 

The Development Requires

curl http

23/10 2014

0.7.1

0.7.1.0 https://github.com/sylouuu/php-curl

Lightweight PHP cURL wrapper

  Sources   Download

MIT

The Requires

  • php >5.3.0

 

The Development Requires

curl http

01/08 2014

0.7.0

0.7.0.0 https://github.com/sylouuu/php-curl

Lightweight PHP cURL wrapper

  Sources   Download

MIT

The Requires

  • php >5.3.0

 

The Development Requires

curl http

30/05 2014

0.6.1

0.6.1.0 https://github.com/sylouuu/php-curl

Lightweight PHP cURL wrapper

  Sources   Download

MIT

The Requires

  • php >5.3.0

 

The Development Requires

curl http

22/05 2014

0.6.0

0.6.0.0 https://github.com/sylouuu/php-curl

Lightweight PHP cURL wrapper

  Sources   Download

MIT

The Requires

  • php >5.3.0

 

The Development Requires

curl http

20/05 2014

0.5.0

0.5.0.0 https://github.com/sylouuu/php-curl

Lightweight PHP cURL wrapper

  Sources   Download

MIT

The Requires

  • php >5.3.0

 

The Development Requires

curl http

20/05 2014

0.5.0-rc1

0.5.0.0-RC1 https://github.com/sylouuu/php-curl

Lightweight PHP cURL wrapper

  Sources   Download

MIT

The Requires

  • php >5.3.0

 

The Development Requires

curl http

13/05 2014

0.4.0

0.4.0.0 https://github.com/sylouuu/php-rest-client

Lightweight PHP cURL wrapper

  Sources   Download

MIT

The Requires

 

api rest http

09/05 2014

0.3.0

0.3.0.0 https://github.com/sylouuu/php-rest-client

Submit REST requests from PHP

  Sources   Download

MIT

The Requires

 

api rest http

06/04 2014

0.2.1

0.2.1.0 https://github.com/sylouuu/php-rest-client

Submit REST requests over HTTP

  Sources   Download

MIT

The Requires

 

api rest http

04/04 2014

0.2.0

0.2.0.0 https://github.com/sylouuu/php-rest-client

Submit REST requests over HTTP

  Sources   Download

MIT

The Requires

 

api rest http

01/04 2014

0.1.0

0.1.0.0 https://github.com/sylouuu/php-rest-client

Submit REST requests over HTTP

  Sources   Download

MIT

The Requires

 

api rest http