Skip to content

Commit 98ceee0

Browse files
committed
Update example
1 parent 721a848 commit 98ceee0

File tree

7 files changed

+144
-11
lines changed

7 files changed

+144
-11
lines changed

README.md

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,58 @@ This library allows you to use any async rust library from PHP, asynchronously.
66

77
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.
88

9-
## Usage
9+
## Example
10+
11+
Here's an example, using the async Rust [reqwest](https://docs.rs/reqwest/latest/reqwest/) library to make asynchronous HTTP requests from PHP:
12+
13+
```php
14+
<?php
15+
16+
use Reqwest\Client;
17+
18+
use function Amp\async;
19+
use function Amp\Future\await;
20+
21+
require 'vendor/autoload.php';
22+
23+
Client::init();
24+
25+
$test = function (string $url): void {
26+
$t = time();
27+
echo "Making async parallel reqwest to $url (time $t)...".PHP_EOL;
28+
$t = time() - $t;
29+
echo "Got response from $url after ~".$t." seconds!".PHP_EOL;
30+
};
31+
32+
$futures = [];
33+
$futures []= async($test(...), "https://httpbin.org/delay/5");
34+
$futures []= async($test(...), "https://httpbin.org/delay/5");
35+
$futures []= async($test(...), "https://httpbin.org/delay/5");
36+
37+
await($futures);
38+
```
39+
40+
Usage:
41+
42+
```bash
43+
cd examples/reqwest && \
44+
cargo build && \
45+
composer update && \
46+
php -d extension=../../target/debug/libexample_reqwest.so test.php
47+
```
48+
49+
Result:
50+
51+
```
52+
Making async parallel reqwest to https://httpbin.org/delay/5 (time 1693160463)...
53+
Making async parallel reqwest to https://httpbin.org/delay/5 (time 1693160463)...
54+
Making async parallel reqwest to https://httpbin.org/delay/5 (time 1693160463)...
55+
Got response from https://httpbin.org/delay/5 after ~6 seconds!
56+
Got response from https://httpbin.org/delay/5 after ~6 seconds!
57+
Got response from https://httpbin.org/delay/5 after ~6 seconds!
58+
```
59+
60+
See the [source code](https://github.com/danog/php-tokio/tree/master/examples/reqwest) of the example for more info on how it works!
1061

1162
## Built with php-tokio
1263

examples/reqwest/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ crate-type = ["cdylib"]
1111
[dependencies]
1212
php-tokio = "^0.1.2"
1313
nicelocal-ext-php-rs = { version = "^0.10.4", features = ["anyhow"] }
14-
reqwest = "^0.11"
14+
reqwest = "^0.11"
15+
anyhow = "^1.0"

examples/reqwest/README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# php-tokio example
2+
3+
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:
4+
5+
```php
6+
<?php
7+
8+
use Reqwest\Client;
9+
10+
use function Amp\async;
11+
use function Amp\Future\await;
12+
13+
require 'vendor/autoload.php';
14+
15+
Client::init();
16+
17+
$test = function (string $url): void {
18+
$t = time();
19+
echo "Making async parallel reqwest to $url (time $t)...".PHP_EOL;
20+
$t = time() - $t;
21+
echo "Got response from $url after ~".$t." seconds!".PHP_EOL;
22+
};
23+
24+
$futures = [];
25+
$futures []= async($test(...), "https://httpbin.org/delay/5");
26+
$futures []= async($test(...), "https://httpbin.org/delay/5");
27+
$futures []= async($test(...), "https://httpbin.org/delay/5");
28+
29+
await($futures);
30+
```
31+
32+
Usage:
33+
34+
```bash
35+
cd examples/reqwest && \
36+
cargo build && \
37+
composer update && \
38+
php -d extension=../../target/debug/libexample_reqwest.so test.php
39+
```
40+
41+
Result:
42+
43+
```
44+
Making async parallel reqwest to https://httpbin.org/delay/5 (time 1693160463)...
45+
Making async parallel reqwest to https://httpbin.org/delay/5 (time 1693160463)...
46+
Making async parallel reqwest to https://httpbin.org/delay/5 (time 1693160463)...
47+
Got response from https://httpbin.org/delay/5 after ~6 seconds!
48+
Got response from https://httpbin.org/delay/5 after ~6 seconds!
49+
Got response from https://httpbin.org/delay/5 after ~6 seconds!
50+
```
51+
52+
See the [source code](https://github.com/danog/php-tokio/tree/master/examples/reqwest) of the example for more info on how it works!

examples/reqwest/composer.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
{
22
"require": {
3-
"revolt/event-loop": "^1.0"
3+
"revolt/event-loop": "^1.0",
4+
"amphp/amp": "^3.0"
5+
},
6+
"autoload": {
7+
"psr-4": {
8+
"Reqwest\\": "lib"
9+
}
410
}
511
}

examples/reqwest/lib/Client.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22

33
namespace Reqwest;
44

5+
use Revolt\EventLoop;
6+
57
final class Client {
68
private static ?string $id = null;
79

8-
public static function register(): void {
10+
public static function init(): void {
911
if (self::$id !== null) {
1012
return;
1113
}
1214

13-
$f = fopen("php://fd/".\reqwest_async_init(), 'r+');
15+
$f = fopen("php://fd/".\Client::init(), 'r+');
1416
stream_set_blocking($f, false);
15-
self::$id = EventLoop::onReadable($f, fn () => \reqwest_async_wakeup());
17+
self::$id = EventLoop::onReadable($f, fn () => \Client::wakeup());
1618
}
1719

1820
public static function reference(): void{
@@ -23,6 +25,6 @@ public static function unreference(): void {
2325
}
2426

2527
public static function __callStatic(string $name, array $args): mixed {
26-
return \Client::$name($args);
28+
return \Client::$name(...$args);
2729
}
2830
}

examples/reqwest/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ impl Client {
1313
pub fn wakeup() -> PhpResult<()> {
1414
EventLoop::wakeup()
1515
}
16-
pub async fn get(url: &str) -> String {
17-
reqwest::get("https://www.rust-lang.org")
16+
pub async fn get(url: &str) -> anyhow::Result<String> {
17+
Ok(reqwest::get(url)
1818
.await?
1919
.text()
2020
.await?
21+
)
2122
}
2223
}
2324

examples/reqwest/test.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
11
<?php
22

3-
require 'vendor/autoload.php'
3+
use Reqwest\Client;
44

5-
\Reqwest\Client::init();
5+
use function Amp\async;
6+
use function Amp\Future\await;
7+
8+
require 'vendor/autoload.php';
9+
10+
Client::init();
11+
12+
$test = function (string $url): void {
13+
$t = time();
14+
echo "Making async parallel reqwest to $url (time $t)...".PHP_EOL;
15+
var_dump(Client::get($url));
16+
$t = time() - $t;
17+
echo "Got response from $url after ~".$t." seconds!".PHP_EOL;
18+
};
19+
20+
$futures = [];
21+
$futures []= async($test(...), "https://httpbin.org/delay/5");
22+
$futures []= async($test(...), "https://httpbin.org/delay/5");
23+
$futures []= async($test(...), "https://httpbin.org/delay/5");
24+
25+
await($futures);

0 commit comments

Comments
 (0)