PHP Classes
elePHPant
Icontem

React PHP HTTP Client: Process HTTP request responses asynchronously

Recommend this page to a friend!
  Info   View files Example   View files View files (8)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2019-04-03 (8 months ago) RSS 2.0 feedNot yet rated by the usersTotal: 106 All time: 9,198 This week: 357Up
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
  Performance   Level  
Name: Sergey Shuchkin <contact>
Classes: 3 packages by
Country: Russian Federation Russian Federation
Age: 38
All time rank: 703 in Russian Federation Russian Federation
Week rank: 9 Up2 in Russian Federation Russian Federation Up
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 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

 Version Control Unique User Downloads Download Rankings  
 100%
Total:106
This week:0
All time:9,198
This week:357Up