I'm very happy to announce the first release of https://github.com/danog/php-tokio: you can now use any async Rust library from PHP!
It's fully integrated with revolt: this allows full compatibility with amphp, PSL and reactphp, and with any Rust library powered by tokio.rs.
Here's an example from the repo, using the async Rust reqwest library to make asynchronous HTTP requests from PHP:
<?phpI'm really excited for the potential usecases of php-tokio, and its implications for the PHP async ecosystem.
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);
I've created this library because I'm building an async MongoDB client for PHP (https://github.com/Nicelocal/mongo-php-async-driver), and realized that using the official rust mongodb library directly from PHP would be way easier (and faster!) than writing a MongoDB client in pure PHP :)
P.S. Here's the link to the hackernews thread I created for the occasion :)
https://news.ycombinator.com/item?id=37285471
>>Click here to continue<<