2017 © Pedro Peláez
 

library http-helper

Object oriented cURL in PHP.

image

novotnyj/http-helper

Object oriented cURL in PHP.

  • Monday, December 14, 2015
  • by novotnyj
  • Repository
  • 1 Watchers
  • 1 Stars
  • 23 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 10 Versions
  • 0 % Grown

The README.md

HttpHelper

This PHP library was created as an alternative to the pecl_http. Goal was to make working with cURL easier and object oriented., (*1)

Basic methods has names similar to pecl_http HttpRequest. This should make switching between pecl_http and HttpHelper easier., (*2)

HttpHelper\Request is using cURL - make sure that you have cURL extension enabled in php.ini, (*3)

Requirements

HttpHelper requires PHP 5.3.1 or later with php5-curl installed., (*4)

Instalation

HttpHelper can be installed using Composer. Package can be found in Packagist repository., (*5)

composer require novotnyj/http-helper

Usage examples

Get request

You can send HTTP request simillar to pecl_http:, (*6)

use HttpHelper\Request;

$request = new Request("http://www.google.com");
$request->send();
if ($request->getResponseCode() == 200) {
    echo $request->getResponseBody();
}

Or you can use HttpHelper\Response object returned by send method. Also wrap send method in try/catch block in case something went wrong:, (*7)

...
try {
    $response = $request->send();
    if ($response->getCode() == 200) {
        echo $response->getBody();
    }
} catch (RequestException $e) {
    echo $e->getMessage();
}

Post request

To send a post request change method to POST, (*8)

use HttpHelper\Request;

$request = new Request("http://www.foo.com/sign/in", Request::POST);
$request->setPostFields(array(
    'login' => 'user',
    'password' => 'p4ssW0rd'
));
try {
    $response = $request->send();
    if ($response->getCode() == 200) {
        echo $response->getBody();
    }
} catch (RequestException $e) {
    echo $e->getMessage();
}

To send a file using POST request:, (*9)

...
$request->setMethod(Request::POST);
$request->setPostFields(array(
    'upload' => '@/absolute/path/to/file.ext'
));
...

To send a JSON POST request:, (*10)

...
$request->setMethod(Request::POST);
$request->setJSON(array(
    'login' => 'foo'
));
...

Follow redirects

To enable automatic following Location header (for 301, 302 and 303 response codes):, (*11)

...
$request->enableRedirects();
...

By default this will follow Location only 20 times. After that RequestException will be raised from send method. You can pass different limit as parameter to enableRedirects (0 means follow Location infinite times). To follow Location for 999 times:, (*12)

$request->enableRedirects(999);

Note: This function is not using CURLOPT_FOLLOWLOCATION, so you should be fine even with open_basedir or safe_mode in your php.ini, (*13)

Use response cookies

To enable automatic using of response cookies for next request:, (*14)

...
$request->enableCookies();
...

NOTE: filtering response cookies by expiration date is not implemented yet., (*15)

The Versions

14/12 2015

dev-master

9999999-dev https://github.com/novotnyj/http-helper

Object oriented cURL in PHP.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.1
  • ext-curl *

 

The Development Requires

by Jan Novotny

php http

26/04 2015

2.0.0.x-dev

2.0.0.9999999-dev https://github.com/novotnyj/http-helper

Object oriented cURL in PHP.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.1
  • ext-curl *

 

The Development Requires

by Jan Novotny

php http

26/04 2015

2.0.0

2.0.0.0 https://github.com/novotnyj/http-helper

Object oriented cURL in PHP.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.1
  • ext-curl *

 

The Development Requires

by Jan Novotny

php http

22/04 2015

1.0.6

1.0.6.0 https://github.com/novotnyj/http-helper

PHP library to help working with HTTP requests and responses without pecl_http.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.1
  • ext-curl *

 

The Development Requires

by Jan Novotny

php http

01/02 2015

1.0.5

1.0.5.0 https://github.com/novotnyj/http-helper

PHP library to help working with HTTP requests and responses without pecl_http.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.1
  • ext-curl *

 

The Development Requires

by Jan Novotny

php http

22/08 2014

1.0.4

1.0.4.0 https://github.com/novotnyj/http-helper

PHP library to help working with HTTP requests and responses without pecl_http.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.1
  • ext-curl *

 

The Development Requires

by Jan Novotny

php http

31/07 2014

1.0.3

1.0.3.0 https://github.com/novotnyj/http-helper

PHP library to help working with HTTP requests and responses without pecl_http.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.1
  • ext-curl *

 

by Jan Novotny

php http

31/07 2014

dev-devel

dev-devel https://github.com/novotnyj/http-helper

PHP library to help working with HTTP requests and responses without pecl_http.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.1
  • ext-curl *

 

by Jan Novotny

php http

07/02 2014

1.0.1

1.0.1.0 https://github.com/novotnyj/http-helper

PHP library to help working with HTTP requests and responses without pecl_http.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.1
  • ext-curl *

 

by Jan Novotny

php http

07/02 2014

1.0.0

1.0.0.0 https://github.com/novotnyj/http-helper

PHP library to help working with HTTP requests and responses without pecl_http.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.1
  • ext-curl *

 

by Jan Novotny

php http