2017 © Pedro Peláez
 

library http

The Neutrino Http package.

image

neutrino/http

The Neutrino Http package.

  • Wednesday, May 3, 2017
  • by Ark4ne
  • Repository
  • 1 Watchers
  • 0 Stars
  • 4 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

Http Client library using Curl or HttpStream., (*1)

Basic

$provider->get($url, $parameters, $options);
$provider->post($url, $parameters, $options);
$provider->delete($url, $parameters, $options);
$provider->put($url, $parameters, $options);
$provider->head($url, $parameters, $options);
$provider->patch($url, $parameters, $options);

$provider->request($method, $url, $parameters, $options);

$url Contain the url to call. $parameters Contain the parameters to send. $options Contain the options of the request., (*2)

$options = [
    // Headers to send
    'headers' => [],
    // Retrieve the full response (Header + Body)
    'full' => true,
    // Make a JsonRequest (Only for POST, PUT, PATCH methods)
    'json' => true,
];

Provider

Curl

require curl extension., (*3)

How use :

use \Neutrino\Http\Provider\Curl as HttpCurl;
use \Neutrino\Http\Method;

$curl = new HttpCurl;

$response = $curl
    ->get('http://www.google.com', ['foo' => 'bar'], ['Accept' => 'text/plain'])
    ->send();

$response->code; // HTTP Status Code

Curl\Streaming

Curl\Stream allows you to work with large queries, by recovers content part by part., (*4)

How use :

use \Neutrino\Http\Provider\Curl\Streaming as HttpCurlStream;
use \Neutrino\Http\Method;

$curl = new HttpCurlStream;

$response = $curl
    ->get('http://www.google.com')
    ->on(HttpCurlStream::EVENT_START, function (HttpCurlStream $curl) {
        // Start to download response body
        // Header are fully loaded when the event are raised
    })
    ->on(HttpCurlStream::EVENT_PROGRESS, function (HttpCurlStream $curl, $content) {
        // Download progress
        // $content contain the response part
    })
    ->send();

Transfer huge data, without overloading the php memory :, (*5)

$curl
    ->get('http://www.google.com')
    ->on(HttpCurlStream::EVENT_START, function (HttpCurlStream $curl) {
        if ($curl->getResponse()->header->has('Content-Length')) {
            header('Content-Length: ' . $curl->getResponse()->header->get('Content-Length'));
        }
    })
    ->on(HttpCurlStream::EVENT_PROGRESS, function (HttpCurlStream $curl, $content) {
        echo $content;
        ob_flush();
        flush();
        // => Direct echo contents & flush the output (free memory)
    })
    ->send();

Download huge file, without overloading the php memory :, (*6)

$resource = fopen($path, 'w');

$curl
    ->get('http://www.google.com')
    ->on(HttpCurlStream::EVENT_PROGRESS, function (HttpCurlStream $curl, $content) use ($resource) {
        fwrite($resource, $content, strlen($content));
    })
    ->send();

fclose($resource);

StreamContext

StreamContext make HTTP call via the php wrapper., (*7)

This require you have "allow_url_fopen" configuration value set to '1'., (*8)

How use :

use \Neutrino\Http\Provider\StreamContext as HttpStreamCtx;
use \Neutrino\Http\Method;

$streamCtx = new HttpStreamCtx;

$response = $streamCtx
    ->get('http://www.google.com', ['foo' => 'bar'], ['headers' => ['Accept' => 'text/plain']])
    ->send();

$response->code; // HTTP Status Code

StreamContext\Streaming

Such as Curl\Streaming, StreamContext\Streaming allows you to work with large queries, by recovers content part by part., (*9)

How use :

use \Neutrino\Http\Provider\StreamContext\Streaming as HttpStreamCtxStreaming;
use \Neutrino\Http\Method;

$streamCtx = new HttpStreamCtxStreaming;

$response = $streamCtx
    ->get('http://www.google.com')
    ->on(HttpStreamCtxStreaming::EVENT_START, function (HttpStreamCtxStreaming $streamCtx) {
        // Start to download response body
        // Header are fully loaded when the event are raised
    })
    ->on(HttpStreamCtxStreaming::EVENT_PROGRESS, function (HttpStreamCtxStreaming $streamCtx, $content) {
        // Download progress
        // $content contain the response part
    })
    ->send();

Auth

Authentication is a request component., (*10)

Auth\Basic

Auth\Basic provides the elements to configure a call with an Basic Authorization., (*11)

How use :

use \Neutrino\Http\Auth\Basic as AuthBasic;
use \Neutrino\Http\Provider\StreamContext as HttpStreamCtx;
use \Neutrino\Http\Method;

$streamCtx = new HttpStreamCtx;

$response = $streamCtx
    ->get('http://www.google.com')
    ->setAuth(new AuthBasic('user', 'pass'))
    ->send();

Auth\Curl

Specific for Curl provider., (*12)

Auth\Curl provides the elements to build a call with Curl Auth., (*13)

How use :

use \Neutrino\Http\Auth\Curl as AuthCurl;
use \Neutrino\Http\Provider\Curl as HttpCurl;
use \Neutrino\Http\Method;

$curl = new HttpCurl;

$response = $curl
    ->get('http://www.google.com')
    ->setAuth(new AuthCurl(CURLAUTH_BASIC | CURLAUTH_DIGEST, 'user', 'pass'))
    ->send();

Custom Auth Component

You can easily make your own Auth Component :, (*14)

namespace MyLib\Http\Auth;

use Neutrino\Http\Request;
use Neutrino\Http\Contract\Request\Component;

class Hmac implements Component
{
    private $id;
    private $value;

    public function __construct($id, $value)
    {
        $this->id = $id;
        $this->value = $value;
    }

    public function build(Request $request)
    {
        $date = date('D, d M Y H:i:s', time());
        $signature = urlencode(base64_encode(hash_hmac('sha1', "date: $date", $this->value, true)));

        $request
            ->setHeader('Date', $date)
            ->setHeader('Authorization', 'Signature keyId="' . $this->id . '",algorithm="hmac-sha1",signature="' . $signature . '"');
    }
}

```php use \MyLib\Http\Auth\Hmac as AuthHmac; use \Neutrino\Http\Provider\Curl as HttpCurl; use \Neutrino\Http\Method;, (*15)

$curl = new HttpCurl;, (*16)

$response = $curl ->get('http://www.google.com') ->setAuth(new AuthHmac('key_id', 'key_value')) ->send();, (*17)


# Response ## Basic ```php $response->code; // HTTP Status Code $response->status; // HTTP Status Message $response->header; // Response Headers $response->body; // Response Body

Provider Info

$response->errorCode; // Provider Error Code
$response->error;     // Provider Error Message
$response->providerDatas; // All Provider Information (if available)

Parse

use \Neutrino\Http\Parser;

// Json Body => Object
$jsonObject = $response->parse(Parser\Json::class)->data;

// Xml Body => SimpleXMLElement
$xmlElement = $response->parse(Parser\Xml::class)->data;

// Xml Body => array
$xmlArray = $response->parse(Parser\XmlArray::class)->data;

// Other exemple : (PHP7)
$response->parse(new class implements Parser\Parserize
{
    public function parse($body)
    {
        return unserialize($body);
    }
});

$response->data; // Unserialized body

The Versions

03/05 2017

1.0.x-dev

1.0.9999999.9999999-dev https://phalcon-nucleon.github.io

The Neutrino Http package.

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Ark4ne (Guillaume Allegret)

api curl json xml rest http restful web service requests stream http client streaming stream context nucleon neutrino

02/05 2017

dev-master

9999999-dev https://phalcon-nucleon.github.io

The Neutrino Http package.

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Ark4ne (Guillaume Allegret)

api curl json xml rest http restful web service requests stream http client streaming stream context nucleon neutrino

02/05 2017

0.1.0-RC2

0.1.0.0-RC2 https://phalcon-nucleon.github.io

The Neutrino Http package.

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Ark4ne (Guillaume Allegret)

api curl json xml rest http restful web service requests stream http client streaming stream context nucleon neutrino

02/05 2017

0.1.x-dev

0.1.9999999.9999999-dev https://phalcon-nucleon.github.io

The Neutrino Http package.

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Ark4ne (Guillaume Allegret)

api curl json xml rest http restful web service requests stream http client streaming stream context nucleon neutrino

28/04 2017

0.1.0-RC1

0.1.0.0-RC1 https://phalcon-nucleon.github.io

The Neutrino Http package.

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Ark4ne (Guillaume Allegret)

api curl json xml rest http restful web service requests stream http client streaming stream context nucleon neutrino