Skip to content

Commit cb3a43a

Browse files
committed
Add higher level HTTP client examples
1 parent 199195c commit cb3a43a

2 files changed

Lines changed: 98 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
use React\EventLoop\Factory;
4+
5+
require __DIR__ . '/../vendor/autoload.php';
6+
7+
$loop = Factory::create();
8+
9+
// connect to www.google.com:80 (blocking call!)
10+
// for illustration purposes only, should use react/socket instead
11+
$stream = stream_socket_client('tcp://www.google.com:80');
12+
if (!$stream) {
13+
exit(1);
14+
}
15+
stream_set_blocking($stream, false);
16+
17+
// send HTTP request
18+
fwrite($stream, "GET / HTTP/1.1\r\nHost: www.google.com\r\nConnection: close\r\n\r\n");
19+
20+
// wait for HTTP response
21+
$loop->addReadStream($stream, function ($stream) use ($loop) {
22+
$chunk = fread($stream, 64 * 1024);
23+
24+
// reading nothing means we reached EOF
25+
if ($chunk === '') {
26+
echo '[END]' . PHP_EOL;
27+
$loop->removeReadStream($stream);
28+
fclose($stream);
29+
return;
30+
}
31+
32+
echo $chunk;
33+
});
34+
35+
$loop->run();

examples/14-http-client-async.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
use React\EventLoop\Factory;
4+
5+
require __DIR__ . '/../vendor/autoload.php';
6+
7+
$loop = Factory::create();
8+
9+
// resolve hostname before establishing TCP/IP connection (resolving DNS is still blocking here)
10+
// for illustration purposes only, should use react/socket or react/dns instead!
11+
$ip = gethostbyname('www.google.com');
12+
if (ip2long($ip) === false) {
13+
echo 'Unable to resolve hostname' . PHP_EOL;
14+
exit(1);
15+
}
16+
17+
// establish TCP/IP connection (non-blocking)
18+
// for illustraction purposes only, should use react/socket instead!
19+
$stream = stream_socket_client('tcp://' . $ip . ':80', $errno, $errstr, null, STREAM_CLIENT_CONNECT | STREAM_CLIENT_ASYNC_CONNECT);
20+
if (!$stream) {
21+
exit(1);
22+
}
23+
stream_set_blocking($stream, false);
24+
25+
// print progress every 10ms
26+
echo 'Connecting';
27+
$timer = $loop->addPeriodicTimer(0.01, function () {
28+
echo '.';
29+
});
30+
31+
// wait for connection success/error
32+
$loop->addWriteStream($stream, function ($stream) use ($loop, $timer) {
33+
$loop->removeWriteStream($stream);
34+
$loop->cancelTimer($timer);
35+
36+
// check for socket error (connection rejected)
37+
if (stream_socket_get_name($stream, true) === false) {
38+
echo '[unable to connect]' . PHP_EOL;
39+
exit(1);
40+
} else {
41+
echo '[connected]' . PHP_EOL;
42+
}
43+
44+
// send HTTP request
45+
fwrite($stream, "GET / HTTP/1.1\r\nHost: www.google.com\r\nConnection: close\r\n\r\n");
46+
47+
// wait for HTTP response
48+
$loop->addReadStream($stream, function ($stream) use ($loop) {
49+
$chunk = fread($stream, 64 * 1024);
50+
51+
// reading nothing means we reached EOF
52+
if ($chunk === '') {
53+
echo '[END]' . PHP_EOL;
54+
$loop->removeReadStream($stream);
55+
fclose($stream);
56+
return;
57+
}
58+
59+
echo $chunk;
60+
});
61+
});
62+
63+
$loop->run();

0 commit comments

Comments
 (0)