Skip to content

Commit

Permalink
Update example
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Aug 27, 2023
1 parent 721a848 commit 98ceee0
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 11 deletions.
53 changes: 52 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion examples/reqwest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
52 changes: 52 additions & 0 deletions examples/reqwest/README.md
Original file line number Diff line number Diff line change
@@ -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!
8 changes: 7 additions & 1 deletion examples/reqwest/composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
{
"require": {
"revolt/event-loop": "^1.0"
"revolt/event-loop": "^1.0",
"amphp/amp": "^3.0"
},
"autoload": {
"psr-4": {
"Reqwest\\": "lib"
}
}
}
10 changes: 6 additions & 4 deletions examples/reqwest/lib/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -23,6 +25,6 @@ public static function unreference(): void {
}

public static function __callStatic(string $name, array $args): mixed {
return \Client::$name($args);
return \Client::$name(...$args);
}
}
5 changes: 3 additions & 2 deletions examples/reqwest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?
)
}
}

Expand Down
24 changes: 22 additions & 2 deletions examples/reqwest/test.php
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit 98ceee0

Please sign in to comment.