Update example

This commit is contained in:
Daniil Gentili 2023-08-27 20:25:28 +02:00
parent 721a84861a
commit 98ceee07e4
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
7 changed files with 144 additions and 11 deletions

View File

@ -6,7 +6,58 @@ 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.
## Usage
## 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();
$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:
```bash
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](https://github.com/danog/php-tokio/tree/master/examples/reqwest) of the example for more info on how it works!
## Built with php-tokio

View File

@ -11,4 +11,5 @@ crate-type = ["cdylib"]
[dependencies]
php-tokio = "^0.1.2"
nicelocal-ext-php-rs = { version = "^0.10.4", features = ["anyhow"] }
reqwest = "^0.11"
reqwest = "^0.11"
anyhow = "^1.0"

View File

@ -0,0 +1,52 @@
# php-tokio example
Here's a usage example of [php-tokio](https://github.com/danog/php-tokio/), 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();
$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:
```bash
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](https://github.com/danog/php-tokio/tree/master/examples/reqwest) of the example for more info on how it works!

View File

@ -1,5 +1,11 @@
{
"require": {
"revolt/event-loop": "^1.0"
"revolt/event-loop": "^1.0",
"amphp/amp": "^3.0"
},
"autoload": {
"psr-4": {
"Reqwest\\": "lib"
}
}
}

View File

@ -2,17 +2,19 @@
namespace Reqwest;
use Revolt\EventLoop;
final class Client {
private static ?string $id = null;
public static function register(): void {
public static function init(): void {
if (self::$id !== null) {
return;
}
$f = fopen("php://fd/".\reqwest_async_init(), 'r+');
$f = fopen("php://fd/".\Client::init(), 'r+');
stream_set_blocking($f, false);
self::$id = EventLoop::onReadable($f, fn () => \reqwest_async_wakeup());
self::$id = EventLoop::onReadable($f, fn () => \Client::wakeup());
}
public static function reference(): void{
@ -23,6 +25,6 @@ final class Client {
}
public static function __callStatic(string $name, array $args): mixed {
return \Client::$name($args);
return \Client::$name(...$args);
}
}

View File

@ -13,11 +13,12 @@ impl Client {
pub fn wakeup() -> PhpResult<()> {
EventLoop::wakeup()
}
pub async fn get(url: &str) -> String {
reqwest::get("https://www.rust-lang.org")
pub async fn get(url: &str) -> anyhow::Result<String> {
Ok(reqwest::get(url)
.await?
.text()
.await?
)
}
}

View File

@ -1,5 +1,25 @@
<?php
require 'vendor/autoload.php'
use Reqwest\Client;
\Reqwest\Client::init();
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;
var_dump(Client::get($url));
$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);