PHP Classes
elePHPant
Icontem

React PHP HTTP Client: Process HTTP request responses asynchronously

Recommend this page to a friend!

  Author Author  
Name: Sergey Shuchkin <contact>
Classes: 3 packages by
Country: Russian Federation Russian Federation


  Detailed description   Download Download .zip .tar.gz  
This class can process HTTP request responses asynchronously.

It uses React PHP to send HTTP requests and set callback functions to process the responses asynchronously once the HTTP servers return them.

The class can send regular HTTP GET, POST, PUT and DELETE requests, handle redirection and chunked response encoding.

Details

react-http-client

ReactPHP async HTTP client, minimal dependencies: https://reactphp.org/

Basic Usage

$loop = \React\EventLoop\Factory::create();

$http = new \Shuchkin\ReactHTTP\Client( $loop );

$http->get( 'https://tools.ietf.org/rfc/rfc2068.txt' )->then(
	function( $content ) {
		echo $content;
	},
	function ( \Exception $ex ) {
		echo 'HTTP error '.$ex->getCode().' '.$ex->getMessage();
	}
);

$loop->run();

Post

$loop = \React\EventLoop\Factory::create();

$http = new \Shuchkin\ReactHTTP\Client( $loop );

$http->post( 'https://reqres.in/api/users', '{"name": "morpheus","job": "leader"}' )->then(
	function ( $content ) {
		echo $content;
	},
	function ( \Exception $ex ) {
		echo 'HTTP error '.$ex->getCode().' '.$ex->getMessage();
	}
);

$loop->run();

// {"name":"morpheus","job":"leader","id":"554","createdAt":"2018-12-17T10:31:29.469Z"}

Send headers

$loop = \React\EventLoop\Factory::create();
$http = new \Shuchkin\ReactHTTP\Client( $loop );

$http->get('https://jigsaw.w3.org/HTTP/TE/foo.txt',['User-Agent' => 'ReactPHP Awesome'] )->then(
	function ( $content ) {
		echo $content;
	},
	function ( \Exception $ex ) {
		echo 'HTTP error '.$ex->getCode().' '.$ex->getMessage();
	}
);
$loop->run();																					

Read chunks

$loop = \React\EventLoop\Factory::create();

$http = new \Shuchkin\ReactHTTP\Client( $loop );
$http->get( 'https://jigsaw.w3.org/HTTP/ChunkedScript' )->then(
	function () {
		echo PHP_EOL . 'Mission complete';
	},
	function ( \Exception $ex ) {
		echo 'ERROR '.$ex->getCode().' '.$ex->getMessage();
	}
);

$http->on('chunk', function( $chunk ) {
	echo PHP_EOL.'-- CHUNK='.$chunk;
});

$loop->run();

Get headers & debug

$loop = \React\EventLoop\Factory::create();

$http = new \Shuchkin\ReactHTTP\Client( $loop );

$http->request('GET','https://reqres.in/api/users')->then(
	function( \Shuchkin\ReactHTTP\Client $client ) {
		// dump response headers
		print_r( $client->headers );
		// dump content
		echo PHP_EOL . $client->content;
	},
	function ( \Exception $ex ) {
		echo 'ERROR '.$ex->getCode().' '.$ex->getMessage();
	}
);
// enable debug mode
$http->on('debug', function( $s ) { echo trim($s).PHP_EOL; } );
$loop->run();

Install

The recommended way to install this library is through Composer. New to Composer?

This will install the latest supported version:

$ composer require shuchkin/react-http-client

  Classes of Sergey Shuchkin  >  React PHP HTTP Client  >  Download Download .zip .tar.gz  >  Support forum Support forum  >  Blog Blog  >  RSS 1.0 feed RSS 2.0 feed Latest changes  
Name: React PHP HTTP Client
Base name: react-http-client
Description: Process HTTP request responses asynchronously
Version: -
PHP version: 5
License: MIT/X Consortium License
 
  Groups   Applications   Files Files  

  Groups  
Group folder image HTTP HTTP protocol clients, headers and cookies View top rated classes
Group folder image PHP 5 Classes using PHP 5 specific features View top rated classes


  Applications that use this package  
No pages of applications that use this class were specified.

Add link image If you know an application of this package, send a message to the author to add a link here.

  Files folder image Files  
File Role Description
Files folder imageexamples (4 files)
Files folder imagesrc (1 file)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  examples  
File Role Description
  Accessible without login Plain text file 01-basic-usage.php Example Example script
  Accessible without login Plain text file 02-headers.php Example Example script
  Accessible without login Plain text file 03-chunked-encoding.php Example Example script
  Accessible without login Plain text file 04-post.php Example Example script

  Files folder image Files  /  src  
File Role Description
  Plain text file Client.php Class Class source

Download Download all files: react-http-client.tar.gz react-http-client.zip
NOTICE: if you are using a download manager program like 'GetRight', please Login before trying to download this archive.