PHP Classes
elePHPant
Icontem

Nano REST: Send REST requests to Web servers using Curl

Recommend this page to a friend!
  Info   View files Documentation   View files View files (27)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2017-12-11 (7 months ago) RSS 2.0 feedNot yet rated by the usersTotal: 133 All time: 8,720 This week: 400Up
Version License PHP version Categories
php-nano-rest 1.1.0MIT/X Consortium ...7.1HTTP, Web services, PHP 7
Description Author

This package can send REST requests to Web servers using CURL.

It can compose a HTTP request to be sent using the CURL extension to a given Web server.

The package can set several types of parameters like the request method, request parameters, request body data, request headers, request content type, etc..

Recommendations

Very Fast Curl class
The fastest Curl class

  Performance   Level  
Name: Sergey Karavay <contact>
Classes: 3 packages by
Country: Belarus Belarus
Age: 28
All time rank: 354111 in Belarus Belarus
Week rank: 748 Up2 in Belarus Belarus Up
Innovation award
Innovation award
Nominee: 1x

Details

PHP Nano Rest

Latest Stable Version Build Status Maintainability Test Coverage Scrutinizer Code Quality SensioLabs Insight License composer.lock Total Downloads

Easy-to-use self-containing lightweight package to deal with cURL requests.

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.

Requirements

  • PHP >= 7.1;
  • cURL extension.

Installation

Require the package using command line:

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

or just put a new dependency in your existing composer.json and run composer install after that:

"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.

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.

Please take a look at examples below, which may clarify everything.

POST some data to endpoint

require './vendor/autoload.php';

$nanoRest = new NanoRest();

//explicitly set expected response type
$nanoRest->setResponseContext(ResponseContext::getByType(ResponseContext::RESPONSE_TYPE_JSON));

//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'
    ]);

$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.

Change the way how request query is generated

By default http_build_query encodes arrays using php square brackets syntax, like this:

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

But sometimes you'll want it to work like this instead:

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

Or even in some other custom-defined way.

That's why setEncodeArraysUsingDuplication and setHttpQueryCustomProcessor methods were added to RequestContext:

$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

$url = "http://some.url";
$data = ['text' => [1,2,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.

$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

Changelog

To keep track, please refer to CHANGELOG.md.

Contributing

Please refer to CONTRIBUTION.md.

License

Please refer to LICENSE.

Notes

Powered by composer-package-template.

  Files folder image Files  
File Role Description
Files folder imagesrc (1 file, 4 directories)
Files folder imagetests (1 file, 1 directory)
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file cacert.pem Data Auxiliary data
Accessible without login Plain text file CHANGELOG.md Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file CONTRIBUTION.md Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  src  
File Role Description
Files folder imageExceptions (3 files)
Files folder imageRequest (2 files)
Files folder imageResponse (3 files)
Files folder imageSupplemental (3 files)
  Plain text file NanoRest.php Class Class source

  Files folder image Files  /  src  /  Exceptions  
File Role Description
  Plain text file RequestContextException.php Class Class source
  Plain text file ResponseContextException.php Class Class source
  Plain text file TransportException.php Class Class source

  Files folder image Files  /  src  /  Request  
File Role Description
  Plain text file HttpBuildQueryBehavior.php Class Class source
  Plain text file RequestContext.php Class Class source

  Files folder image Files  /  src  /  Response  
File Role Description
  Plain text file DummyResponseContext.php Class Class source
  Plain text file JsonResponseContext.php Class Class source
  Plain text file ResponseContext.php Class Class source

  Files folder image Files  /  src  /  Supplemental  
File Role Description
  Plain text file CurlHelper.php Class Class source
  Plain text file Headers.php Class Class source
  Plain text file HeadersProperty.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imagesuites (3 directories)
  Accessible without login Plain text file bootstrap.php Aux. Auxiliary script

  Files folder image Files  /  tests  /  suites  
File Role Description
Files folder imageintegration (1 file)
Files folder imagemain (3 files)
Files folder imagesupplemental (1 file)

  Files folder image Files  /  tests  /  suites  /  integration  
File Role Description
  Plain text file IntegrationTest.php Class Class source

  Files folder image Files  /  tests  /  suites  /  main  
File Role Description
  Plain text file NanoRestTest.php Class Class source
  Plain text file RequestContextTest.php Class Class source
  Plain text file ResponseContextTest.php Class Class source

  Files folder image Files  /  tests  /  suites  /  supplemental  
File Role Description
  Plain text file HeadersTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:133
This week:0
All time:8,720
This week:400Up