2017 © Pedro Peláez
 

library nano-rest

Minimalistic and self-contained cURL HTTP REST client for PHP

image

gino-pane/nano-rest

Minimalistic and self-contained cURL HTTP REST client for PHP

  • Tuesday, February 13, 2018
  • by GinoPane
  • Repository
  • 1 Watchers
  • 2 Stars
  • 103 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 12 Versions
  • 1 % Grown

The README.md

PHP Nano Rest

Latest Stable Version Build Status Maintainability Test Coverage Scrutinizer Code Quality SensioLabs Insight License Total Downloads, (*1)

Easy-to-use self-containing lightweight package to deal with cURL requests., (*2)

Such packages as Guzzle are great of course, but they are too heavy for small projects. You don't need an overkill package for some easy stuff, and that's when something small and neat may help., (*3)

Requirements

  • PHP >= 7.1;
  • cURL extension.

Installation

Require the package using command line:, (*4)

composer require "gino-pane/nano-rest:1.*"

or just put a new dependency in your existing composer.json and run composer install after that:, (*5)

"require": {
    ...
    "gino-pane/nano-rest": "1.*"
}

Usage

Project's philosophy implies usage of RequestContext and ResponseContext objects. RequestContext aggregates request settings whilst ResponseContext contains response data., (*6)

Response context can be typed. Currently only JsonResponseContext available for JSON responses. Response type must be set explicitly by user. If no response type set, DummyResponseContext will be used., (*7)

Please take a look at examples below, which may clarify everything., (*8)

POST some data to endpoint

require './vendor/autoload.php';

$nanoRest = new NanoRest();

//create request context
$requestContext = (new RequestContext('http://httpbin.org/post')) //pass URL to constructor
    ->setMethod(RequestContext::METHOD_POST) //set request method. GET is default
    ->setRequestParameters([ //set some request parameters. They will be attached to URL
        'foo' => 'bar'
    ])
    ->setData('Hello world!') //set request data for body
    ->setContentType(RequestContext::CONTENT_TYPE_TEXT_PLAIN) //being set by default
    ->setHeaders([ // set some headers for request
        'bar' => 'baz'
    ])
    ->setResponseContextClass(JsonResponseContext::class); //explicitly set expected response type

$responseContext = $nanoRest->sendRequest($requestContext);

$responseContext->getHttpStatusCode(); //200
$responseContext->hasHttpError() //false

$responseContext->getArray();

/**
array(8) {
  'args' =>
  array(1) {
    'foo' =>
    string(3) "bar"
  }
  'data' =>
  string(12) "Hello world!"
  'files' =>
  array(0) {
  }
  'form' =>
  array(0) {
  }
  'headers' =>
  array(8) {
    'Accept' =>
    string(3) "*/*"
    'Accept-Encoding' =>
    string(13) "deflate, gzip"
    'Bar' =>
    string(3) "baz"
    'Connection' =>
    string(5) "close"
    'Content-Length' =>
    string(2) "12"
    'Content-Type' =>
    string(25) "text/plain; charset=UTF-8"
    'Host' =>
    string(11) "httpbin.org"
    'User-Agent' =>
    string(13) "php-nano-rest"
  }
  'json' =>
  NULL
  'origin' =>
  string(12) "93.85.47.181"
  'url' =>
  string(31) "http://httpbin.org/post?foo=bar"
}
*/

RequestContext provides setCurlOption/setCurlOptions which allow to override default cURL options and customize request for all your needs. Please examine source code and provided IntegrationTest carefully to get the whole idea., (*9)

Change the way how request query is generated

By default http_build_query encodes arrays using PHP square brackets syntax, like this:, (*10)

?text[0]=1&text[1]=2&text[2]=3

But sometimes you'll want it to work like this instead:, (*11)

?text=1&text=2&text=3

Or even in some other custom-defined way., (*12)

That's why setEncodeArraysUsingDuplication and setHttpQueryCustomProcessor methods were added to RequestContext:, (*13)

$url = "http://some.url";
$data = ['text' => [1,2,3]];

$request = (new RequestContext($url))
            ->setRequestParameters($data)
            ->setEncodeArraysUsingDuplication(false);

$requestUrl = $request->getRequestUrl(); //http://some.url?text%5B0%5D=1&text%5B1%5D=2&text%5B2%5D=3

$request = (new RequestContext($url))
            ->setRequestParameters($data)
            ->setEncodeArraysUsingDuplication(true);

$requestUrl = $request->getRequestUrl(); //http://some.url?text=1&text=2&text=3

Method setHttpQueryCustomProcessor allows you to set your custom Closure that will be called on HTTP query string so you could process it as you wish. Initial request $data array will be passed to it as a second parameter., (*14)

$url = "http://some.url";
$data = ['text' => [1,2,3]];

$request = (new RequestContext($url))
            ->setRequestParameters($data)
            ->setEncodeArraysUsingDuplication(true);

$request->setHttpQueryCustomProcessor(
    function (string $query, array $data) {
        return str_replace('text', 'data', $query);
    }
);

$requestUrl = $request->getRequestUrl(); //http://some.url?data=1&data=2&data=3

Useful Tools

Running Tests:

php vendor/bin/phpunit

or, (*15)

composer test

Code Sniffer Tool:

php vendor/bin/phpcs --standard=PSR2 src/

or, (*16)

composer psr2check

Code Auto-fixer:

php vendor/bin/phpcbf --standard=PSR2 src/ 

or, (*17)

composer psr2autofix

Building Docs:

php vendor/bin/phpdoc -d "src" -t "docs"

or, (*18)

composer docs

Updating Cacert.pem:

    php bin/update-cacert.php

 or

    composer update-cacert

Changelog

To keep track, please refer to CHANGELOG.md., (*19)

Contributing

Please refer to CONTRIBUTING.md., (*20)

License

Please refer to LICENSE., (*21)

Notes

Powered by composer-package-template, (*22)

The Versions

13/02 2018

dev-master

9999999-dev https://github.com/GinoPane/php-nano-rest

Minimalistic and self-contained cURL HTTP REST client for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

rest http http client rest client php rest minimalistic nano rest

13/02 2018

v1.2.3

1.2.3.0 https://github.com/GinoPane/php-nano-rest

Minimalistic and self-contained cURL HTTP REST client for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

rest http http client rest client php rest minimalistic nano rest

05/02 2018

v1.2.2

1.2.2.0 https://github.com/GinoPane/php-nano-rest

Minimalistic and self-contained cURL HTTP REST client for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

rest http http client rest client php rest minimalistic nano rest

11/01 2018

v1.2.1

1.2.1.0 https://github.com/GinoPane/php-nano-rest

Minimalistic and self-contained cURL HTTP REST client for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

rest http http client rest client php rest minimalistic nano rest

18/12 2017

v1.2.0

1.2.0.0 https://github.com/GinoPane/php-nano-rest

Minimalistic and self-contained cURL HTTP REST client for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

rest http http client rest client php rest minimalistic nano rest

14/12 2017

v1.1.3

1.1.3.0 https://github.com/GinoPane/php-nano-rest

Minimalistic and self-contained cURL HTTP REST client for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

rest http http client rest client php rest minimalistic nano rest

12/12 2017

v1.1.2

1.1.2.0 https://github.com/GinoPane/php-nano-rest

Minimalistic and self-contained cURL HTTP REST client for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

rest http http client rest client php rest minimalistic nano rest

12/12 2017

v1.1.1

1.1.1.0 https://github.com/GinoPane/php-nano-rest

Minimalistic and self-contained cURL HTTP REST client for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

rest http http client rest client php rest minimalistic nano rest

05/12 2017

v1.1.0

1.1.0.0 http://github.com/GinoPane/PhpNanoRest

Minimalistic and self-contained cURL HTTP REST client for PHP

  Sources   Download

MIT

The Requires

  • php ^7.1
  • ext-curl *

 

The Development Requires

rest http http client rest client php rest minimalistic nano rest

04/12 2017

v1.0.1

1.0.1.0 http://github.com/GinoPane/PhpNanoRest

Minimalistic and self-contained cURL HTTP REST client for PHP

  Sources   Download

MIT

The Requires

  • php ^7.1
  • ext-curl *

 

The Development Requires

rest http http client rest client php rest minimalistic nano rest

30/11 2017

v1.0.0

1.0.0.0 http://github.com/GinoPane/PhpNanoRest

Minimalistic and self-contained cURL HTTP REST client for PHP

  Sources   Download

MIT

The Requires

  • php ^7.1
  • ext-curl *

 

The Development Requires

rest http http client rest client php rest minimalistic nano rest

20/11 2017

v0.1.0

0.1.0.0 http://github.com/GinoPane/PhpNanoRest

Template composer package for creating other packages

  Sources   Download

dbad

The Requires

  • php ^7.0

 

The Development Requires

rest http rest-client http-client minimalistic php-rest nano-rest