PHP Classes

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    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 115 This week: 1All time: 9,555 This week: 560Up
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: 42
All time rank: 533 in Russian Federation Russian Federation
Week rank: 52 Up2 in Russian Federation Russian Federation Up
Innovation award
Innovation award
Nominee: 1x

Example

<?php

require_once __DIR__ . '/../vendor/autoload.php';

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

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

$http->get( 'https://tools.ietf.org/html/rfc2616' )->then( function ( $content ) {

    echo
$content;

}, function ( \
Exception $ex ) {

    echo
'HTTP error '.$ex->getCode().' '.$ex->getMessage();

} );

//$http->on('debug', function( $s ) { echo trim($s).PHP_EOL; } );

$loop->run();


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:115
This week:1
All time:9,555
This week:560Up