forked from reactphp-legacy/socket-client
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path01-http.php
33 lines (24 loc) · 878 Bytes
/
01-http.php
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
<?php
use React\EventLoop\Factory;
use React\SocketClient\TcpConnector;
use React\SocketClient\DnsConnector;
use React\SocketClient\TimeoutConnector;
use React\SocketClient\ConnectionInterface;
require __DIR__ . '/../vendor/autoload.php';
$loop = Factory::create();
$factory = new \React\Dns\Resolver\Factory();
$resolver = $factory->create('8.8.8.8', $loop);
$tcp = new TcpConnector($loop);
$dns = new DnsConnector($tcp, $resolver);
// time out connection attempt in 3.0s
$dns = new TimeoutConnector($dns, 3.0, $loop);
$dns->create('www.google.com', 80)->then(function (ConnectionInterface $connection) {
$connection->on('data', function ($data) {
echo $data;
});
$connection->on('close', function () {
echo '[CLOSED]' . PHP_EOL;
});
$connection->write("GET / HTTP/1.0\r\nHost: www.google.com\r\n\r\n");
}, 'printf');
$loop->run();