Crates.io | php-tokio |
lib.rs | php-tokio |
version | 0.1.7 |
source | src |
created_at | 2023-08-27 15:46:58.504793 |
updated_at | 2024-02-26 09:03:18.247047 |
description | Use any async Rust library from PHP! |
homepage | https://github.com/danog/php-tokio |
repository | https://github.com/danog/php-tokio |
max_upload_size | |
id | 956163 |
size | 24,537 |
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.
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();
function test(int $delay): void {
$url = "https://httpbin.org/delay/$delay";
$t = time();
echo "Making async reqwest to $url that will return after $delay seconds...".PHP_EOL;
Client::get($url);
$t = time() - $t;
echo "Got response from $url after ~".$t." seconds!".PHP_EOL;
};
$futures = [];
$futures []= async(test(...), 5);
$futures []= async(test(...), 5);
$futures []= async(test(...), 5);
await($futures);
Usage:
cd examples/reqwest && \
cargo build && \
composer update && \
php -d extension=../../target/debug/libexample_reqwest.so test.php
Result:
Making async reqwest to https://httpbin.org/delay/5 that will return after 5 seconds...
Making async reqwest to https://httpbin.org/delay/5 that will return after 5 seconds...
Making async reqwest to https://httpbin.org/delay/5 that will return after 5 seconds...
Got response from https://httpbin.org/delay/5 after ~5 seconds!
Got response from https://httpbin.org/delay/5 after ~5 seconds!
Got response from https://httpbin.org/delay/5 after ~5 seconds!
See the source code of the example for more info on how it works!
Here's a list of async PHP extensions built with php-tokio (add yours by editing this file!):