mirror of
https://github.com/danog/php-tokio.git
synced 2024-12-02 09:38:30 +01:00
Update example
This commit is contained in:
parent
721a84861a
commit
98ceee07e4
53
README.md
53
README.md
@ -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.
|
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
|
## Built with php-tokio
|
||||||
|
|
||||||
|
@ -12,3 +12,4 @@ crate-type = ["cdylib"]
|
|||||||
php-tokio = "^0.1.2"
|
php-tokio = "^0.1.2"
|
||||||
nicelocal-ext-php-rs = { version = "^0.10.4", features = ["anyhow"] }
|
nicelocal-ext-php-rs = { version = "^0.10.4", features = ["anyhow"] }
|
||||||
reqwest = "^0.11"
|
reqwest = "^0.11"
|
||||||
|
anyhow = "^1.0"
|
52
examples/reqwest/README.md
Normal file
52
examples/reqwest/README.md
Normal 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!
|
@ -1,5 +1,11 @@
|
|||||||
{
|
{
|
||||||
"require": {
|
"require": {
|
||||||
"revolt/event-loop": "^1.0"
|
"revolt/event-loop": "^1.0",
|
||||||
|
"amphp/amp": "^3.0"
|
||||||
|
},
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"Reqwest\\": "lib"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,17 +2,19 @@
|
|||||||
|
|
||||||
namespace Reqwest;
|
namespace Reqwest;
|
||||||
|
|
||||||
|
use Revolt\EventLoop;
|
||||||
|
|
||||||
final class Client {
|
final class Client {
|
||||||
private static ?string $id = null;
|
private static ?string $id = null;
|
||||||
|
|
||||||
public static function register(): void {
|
public static function init(): void {
|
||||||
if (self::$id !== null) {
|
if (self::$id !== null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$f = fopen("php://fd/".\reqwest_async_init(), 'r+');
|
$f = fopen("php://fd/".\Client::init(), 'r+');
|
||||||
stream_set_blocking($f, false);
|
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{
|
public static function reference(): void{
|
||||||
@ -23,6 +25,6 @@ final class Client {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static function __callStatic(string $name, array $args): mixed {
|
public static function __callStatic(string $name, array $args): mixed {
|
||||||
return \Client::$name($args);
|
return \Client::$name(...$args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,11 +13,12 @@ impl Client {
|
|||||||
pub fn wakeup() -> PhpResult<()> {
|
pub fn wakeup() -> PhpResult<()> {
|
||||||
EventLoop::wakeup()
|
EventLoop::wakeup()
|
||||||
}
|
}
|
||||||
pub async fn get(url: &str) -> String {
|
pub async fn get(url: &str) -> anyhow::Result<String> {
|
||||||
reqwest::get("https://www.rust-lang.org")
|
Ok(reqwest::get(url)
|
||||||
.await?
|
.await?
|
||||||
.text()
|
.text()
|
||||||
.await?
|
.await?
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,25 @@
|
|||||||
<?php
|
<?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);
|
Loading…
Reference in New Issue
Block a user