forked from clue/reactphp-block
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01-await.php
More file actions
36 lines (28 loc) · 888 Bytes
/
01-await.php
File metadata and controls
36 lines (28 loc) · 888 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
use Clue\React\Block;
require __DIR__ . '/../vendor/autoload.php';
/**
* @param string $url
* @return Psr\Http\Message\ResponseInterface
* @throws Exception when HTTP request fails
*/
function requestHttp($url)
{
// use a unique event loop instance for this operation
$loop = React\EventLoop\Factory::create();
// This example uses an HTTP client
$browser = new React\Http\Browser($loop);
// set up one request
$promise = $browser->get($url);
try {
// keep the loop running (i.e. block) until the response arrives
$result = Block\await($promise, $loop);
// promise successfully fulfilled with $result
return $result;
} catch (Exception $exception) {
// promise rejected with $exception
throw $exception;
}
}
$response = requestHttp('http://www.google.com/');
echo $response->getBody();