Skip to content

Commit 3a7389b

Browse files
authored
Merge pull request #51 from SimonFrings/update
Update README to reactphp/http v1.0.0 and add examples
2 parents 81c03f7 + aa438b2 commit 3a7389b

File tree

6 files changed

+135
-5
lines changed

6 files changed

+135
-5
lines changed

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ into your traditional, synchronous (blocking) application stack.
3333
### Quickstart example
3434

3535
The following example code demonstrates how this library can be used along with
36-
an [async HTTP client](https://github.com/clue/reactphp-buzz) to process two
36+
an [async HTTP client](https://github.com/reactphp/http#client-usage) to process two
3737
non-blocking HTTP requests and block until the first (faster) one resolves.
3838

3939
```php
@@ -44,7 +44,7 @@ function blockingExample()
4444

4545
// this example uses an HTTP client
4646
// this could be pretty much everything that binds to an event loop
47-
$browser = new Clue\React\Buzz\Browser($loop);
47+
$browser = new React\Http\Browser($loop);
4848

4949
// set up two parallel requests
5050
$request1 = $browser->get('http://www.google.com/');
@@ -128,14 +128,16 @@ will throw an `UnexpectedValueException` instead.
128128
```php
129129
try {
130130
$result = Block\await($promise, $loop);
131-
// promise successfully fulfilled with $value
131+
// promise successfully fulfilled with $result
132132
echo 'Result: ' . $result;
133133
} catch (Exception $exception) {
134134
// promise rejected with $exception
135135
echo 'ERROR: ' . $exception->getMessage();
136136
}
137137
```
138138

139+
See also the [examples](examples/).
140+
139141
If no `$timeout` argument is given and the promise stays pending, then this
140142
will potentially wait/block forever until the promise is settled.
141143

@@ -160,6 +162,8 @@ $firstResult = Block\awaitAny($promises, $loop, $timeout);
160162
echo 'First result: ' . $firstResult;
161163
```
162164

165+
See also the [examples](examples/).
166+
163167
This function will only return after ANY of the given `$promises` has been
164168
fulfilled or will throw when ALL of them have been rejected. In the meantime,
165169
the event loop will run any events attached to the same loop.
@@ -195,6 +199,8 @@ $allResults = Block\awaitAll($promises, $loop, $timeout);
195199
echo 'First promise resolved with: ' . $allResults[0];
196200
```
197201

202+
See also the [examples](examples/).
203+
198204
This function will only return after ALL of the given `$promises` have been
199205
fulfilled or will throw when ANY of them have been rejected. In the meantime,
200206
the event loop will run any events attached to the same loop.

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"react/promise-timer": "^1.5"
2424
},
2525
"require-dev": {
26-
"phpunit/phpunit": "^9.3 || ^5.7 || ^4.8.35"
26+
"phpunit/phpunit": "^9.3 || ^5.7 || ^4.8.35",
27+
"react/http": "^1.0"
2728
}
2829
}

examples/01-await.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
use Clue\React\Block;
4+
5+
require __DIR__ . '/../vendor/autoload.php';
6+
7+
/**
8+
* @param string $url
9+
* @return Psr\Http\Message\ResponseInterface
10+
* @throws Exception when HTTP request fails
11+
*/
12+
function requestHttp($url)
13+
{
14+
// use a unique event loop instance for this operation
15+
$loop = React\EventLoop\Factory::create();
16+
17+
// This example uses an HTTP client
18+
$browser = new React\Http\Browser($loop);
19+
20+
// set up one request
21+
$promise = $browser->get($url);
22+
23+
try {
24+
// keep the loop running (i.e. block) until the response arrives
25+
$result = Block\await($promise, $loop);
26+
27+
// promise successfully fulfilled with $result
28+
return $result;
29+
} catch (Exception $exception) {
30+
// promise rejected with $exception
31+
throw $exception;
32+
}
33+
}
34+
35+
$response = requestHttp('http://www.google.com/');
36+
echo $response->getBody();

examples/02-await-any.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
use Clue\React\Block;
4+
5+
require __DIR__ . '/../vendor/autoload.php';
6+
7+
/**
8+
* @param string $url1
9+
* @param string $url2
10+
* @return Psr\Http\Message\ResponseInterface
11+
* @throws Exception when HTTP request fails
12+
*/
13+
function requestHttpFastestOfMultiple($url1, $url2)
14+
{
15+
// use a unique event loop instance for all parallel operations
16+
$loop = React\EventLoop\Factory::create();
17+
18+
// This example uses an HTTP client
19+
$browser = new React\Http\Browser($loop);
20+
21+
// set up two parallel requests
22+
$promises = array(
23+
$browser->get($url1),
24+
$browser->get($url2)
25+
);
26+
27+
try {
28+
// keep the loop running (i.e. block) until the first response arrives
29+
$fasterResponse = Block\awaitAny($promises, $loop);
30+
31+
// promise successfully fulfilled with $fasterResponse
32+
return $fasterResponse;
33+
} catch (Exception $exception) {
34+
// promise rejected with $exception
35+
throw $exception;
36+
}
37+
}
38+
39+
$first = requestHttpFastestOfMultiple('http://www.google.com/', 'http://www.google.co.uk/');
40+
echo $first->getBody();

examples/03-await-all.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
use Clue\React\Block;
4+
5+
require __DIR__ . '/../vendor/autoload.php';
6+
7+
/**
8+
* @param string $url1
9+
* @param string $url2
10+
* @return Psr\Http\Message\ResponseInterface[]
11+
* @throws Exception when HTTP request fails
12+
*/
13+
function requestHttpMultiple($url1, $url2)
14+
{
15+
// use a unique event loop instance for all parallel operations
16+
$loop = React\EventLoop\Factory::create();
17+
18+
// This example uses an HTTP client
19+
$browser = new React\Http\Browser($loop);
20+
21+
// set up two parallel requests
22+
$promises = array(
23+
$browser->get($url1),
24+
$browser->get($url2)
25+
);
26+
27+
try {
28+
// keep the loop running (i.e. block) until all responses arrive
29+
$allResults = Block\awaitAll($promises, $loop);
30+
31+
// promise successfully fulfilled with $allResults
32+
return $allResults;
33+
} catch (Exception $exception) {
34+
// promise rejected with $exception
35+
throw $exception;
36+
}
37+
}
38+
39+
$all = requestHttpMultiple('http://www.google.com/', 'http://www.google.co.uk/');
40+
echo 'First promise resolved with: ' . $all[0]->getBody() . PHP_EOL;
41+
echo 'Second promise resolved with: ' . $all[1]->getBody() . PHP_EOL;

src/functions.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,16 @@ function sleep($time, LoopInterface $loop)
5858
* ```php
5959
* try {
6060
* $result = Block\await($promise, $loop);
61-
* // promise successfully fulfilled with $value
61+
* // promise successfully fulfilled with $result
6262
* echo 'Result: ' . $result;
6363
* } catch (Exception $exception) {
6464
* // promise rejected with $exception
6565
* echo 'ERROR: ' . $exception->getMessage();
6666
* }
6767
* ```
6868
*
69+
* See also the [examples](../examples/).
70+
*
6971
* If no `$timeout` argument is given and the promise stays pending, then this
7072
* will potentially wait/block forever until the promise is settled.
7173
*
@@ -143,6 +145,8 @@ function ($error) use (&$exception, &$rejected, &$wait, $loop) {
143145
* echo 'First result: ' . $firstResult;
144146
* ```
145147
*
148+
* See also the [examples](../examples/).
149+
*
146150
* This function will only return after ANY of the given `$promises` has been
147151
* fulfilled or will throw when ALL of them have been rejected. In the meantime,
148152
* the event loop will run any events attached to the same loop.
@@ -221,6 +225,8 @@ function awaitAny(array $promises, LoopInterface $loop, $timeout = null)
221225
* echo 'First promise resolved with: ' . $allResults[0];
222226
* ```
223227
*
228+
* See also the [examples](../examples/).
229+
*
224230
* This function will only return after ALL of the given `$promises` have been
225231
* fulfilled or will throw when ANY of them have been rejected. In the meantime,
226232
* the event loop will run any events attached to the same loop.

0 commit comments

Comments
 (0)