php-tokio/README.md

68 lines
2.1 KiB
Markdown
Raw Permalink Normal View History

2023-08-27 17:04:51 +02:00
# php-tokio - Use any async Rust library from PHP!
2023-08-27 16:23:04 +02:00
Created by Daniil Gentili ([@danog](https://github.com/danog)).
This library allows you to use any async rust library from PHP, asynchronously.
It's fully integrated with [revolt](https://revolt.run): this allows full compatibility with [amphp](https://amphp.org), [PSL](https://github.com/azjezz/psl) and reactphp.
2023-08-27 20:25:28 +02:00
## Example
Here's an example, using the async Rust [reqwest](https://docs.rs/reqwest/latest/reqwest/) library to make asynchronous HTTP requests from PHP:
```php
<?php
use Reqwest\Client;
use function Amp\async;
use function Amp\Future\await;
require 'vendor/autoload.php';
Client::init();
2023-08-27 20:40:41 +02:00
function test(int $delay): void {
$url = "https://httpbin.org/delay/$delay";
2023-08-27 20:25:28 +02:00
$t = time();
2023-08-27 20:40:41 +02:00
echo "Making async reqwest to $url that will return after $delay seconds...".PHP_EOL;
Client::get($url);
2023-08-27 20:25:28 +02:00
$t = time() - $t;
echo "Got response from $url after ~".$t." seconds!".PHP_EOL;
};
$futures = [];
2023-08-27 20:40:41 +02:00
$futures []= async(test(...), 5);
$futures []= async(test(...), 5);
$futures []= async(test(...), 5);
2023-08-27 20:25:28 +02:00
await($futures);
```
Usage:
```bash
cd examples/reqwest && \
cargo build && \
composer update && \
php -d extension=../../target/debug/libexample_reqwest.so test.php
```
Result:
```
2023-08-27 20:40:41 +02:00
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!
2023-08-27 20:25:28 +02:00
```
See the [source code](https://github.com/danog/php-tokio/tree/master/examples/reqwest) of the example for more info on how it works!
2023-08-27 16:23:04 +02:00
## Built with php-tokio
2023-08-27 20:45:03 +02:00
Here's a list of async PHP extensions built with php-tokio ([add yours by editing this file!](https://github.com/danog/php-tokio/edit/master/README.md)):
2023-08-27 16:23:04 +02:00
- [nicelocal/mongo-php-async-driver](https://github.com/Nicelocal/mongo-php-async-driver) - An async MongoDB PHP extension