Go to file
2023-08-27 20:25:28 +02:00
.vscode First commit 2023-08-27 16:23:04 +02:00
crates/macros Fix license identifiers 2023-08-27 17:38:45 +02:00
examples/reqwest Update example 2023-08-27 20:25:28 +02:00
src Switch to nicelocal-ext-php-rs 2023-08-27 17:36:30 +02:00
.gitignore Add example 2023-08-27 19:32:52 +02:00
Cargo.toml Update deps 2023-08-27 19:52:56 +02:00
LICENSE First commit 2023-08-27 16:23:04 +02:00
README.md Update example 2023-08-27 20:25:28 +02:00

php-tokio - Use any async Rust library from PHP!

Created by Daniil Gentili (@danog).

This library allows you to use any async rust library from PHP, asynchronously.

It's fully integrated with revolt: this allows full compatibility with amphp, PSL and reactphp.

Example

Here's an example, 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!

Built with php-tokio

Here's a list of async PHP extensions built with php-tokio (add yours by editing this file!):