React PHP HTTP Client: Process HTTP request responses asynchronously

Recommend this page to a friend!
  Info   Example   View files (8)   Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 107 All time: 9,292 This week: 327
Version License PHP version Categories
react-http-client 1.0.0MIT/X Consortium ...5HTTP, PHP 5
Description Author

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.

Innovation Award
PHP Programming Innovation award nominee
April 2019
Number 9
React PHP is a PHP framework that makes it possible to implement asynchronous programming in PHP.

This allows PHP applications to perform multiple tasks at the same time using the same script, eventually finishing the tasks in less time and using less server computer memory space.

This class implements an HTTP client using React PHP, thus making it possible to send and process multiple HTTP requests to remote servers, while executing other tasks at the same time.

Manuel Lemos
Picture of Sergey Shuchkin
  Performance   Level  
Name: Sergey Shuchkin <contact>
Classes: 4 packages by
Country: Russian Federation Russian Federation
Age: 38
All time rank: 653 in Russian Federation Russian Federation
Week rank: 11 2 in Russian Federation Russian Federation
Innovation award
Innovation award
Nominee: 1x

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
  Files  
File Role Description
examples (4 files)
src (1 file)
composer.json Data Auxiliary data
LICENSE Lic. License text
README.md Doc. Documentation

  Files  /  examples  
File Role Description
   01-basic-usage.php Example Example script
   02-headers.php Example Example script
   03-chunked-encoding.php Example Example script
   04-post.php Example Example script

  Files  /  src  
File Role Description
   Client.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:107
This week:0
All time:9,292
This week:327

For more information send a message to info at phpclasses dot org.