-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
144 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |