mirror of
https://github.com/danog/php-tokio.git
synced 2024-11-26 12:24:58 +01:00
.. | ||
.cargo | ||
lib | ||
src | ||
Cargo.toml | ||
composer.json | ||
README.md | ||
test.php |
php-tokio example
Here's a usage example of php-tokio, using the async Rust reqwest library to make asynchronous HTTP requests from PHP:
<?php
use Reqwest\Client;
use function Amp\async;
use function Amp\Future\await;
require 'vendor/autoload.php';
Client::init();
$test = function (string $url): void {
$t = time();
echo "Making async parallel reqwest to $url (time $t)...".PHP_EOL;
$t = time() - $t;
echo "Got response from $url after ~".$t." seconds!".PHP_EOL;
};
$futures = [];
$futures []= async($test(...), "https://httpbin.org/delay/5");
$futures []= async($test(...), "https://httpbin.org/delay/5");
$futures []= async($test(...), "https://httpbin.org/delay/5");
await($futures);
Usage:
cd examples/reqwest && \
cargo build && \
composer update && \
php -d extension=../../target/debug/libexample_reqwest.so test.php
Result:
Making async parallel reqwest to https://httpbin.org/delay/5 (time 1693160463)...
Making async parallel reqwest to https://httpbin.org/delay/5 (time 1693160463)...
Making async parallel reqwest to https://httpbin.org/delay/5 (time 1693160463)...
Got response from https://httpbin.org/delay/5 after ~6 seconds!
Got response from https://httpbin.org/delay/5 after ~6 seconds!
Got response from https://httpbin.org/delay/5 after ~6 seconds!
See the source code of the example for more info on how it works!