PHP code example of shuchkin / react-http-client

1. Go to this page and download the library: Download shuchkin/react-http-client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

shuchkin / react-http-client example snippets


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

$http->get( 'http://api.ipify.org' )->then(
	function( $content ) {
		echo $content; // 123.45.67.8
	}
);

$loop->run();

$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"}

$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();																					

$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();

$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();