PHP code example of neutrino / http

1. Go to this page and download the library: Download neutrino/http library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

neutrino / http example snippets


$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);

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

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

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();

$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();

$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);

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

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();

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();

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();

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 . '"');
    }
}

use \MyLib\Http\Auth\Hmac as AuthHmac;
use \Neutrino\Http\Provider\Curl as HttpCurl;
use \Neutrino\Http\Method;

$curl = new HttpCurl;

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

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

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

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