PHP Classes

React PHP HTTP Client: Process HTTP request responses asynchronously

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 115 All time: 9,571 This week: 673Up
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: 43
All time rank: 513 in Russian Federation Russian Federation
Week rank: 14 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 (8)  
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 (8)  /  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 (8)  /  src  
File Role Description
  Plain text file Client.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:115
This week:0
All time:9,571
This week:673Up