forked from reactphp/socket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
12-https-client.php
34 lines (27 loc) · 1.08 KB
/
12-https-client.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
34
<?php
// Simple secure HTTPS client example (for illustration purposes only).
// This shows how a secure TLS connection is established to then send an
// application level protocol message (HTTP).
// Real applications should use react/http-client instead
//
// This simple example only accepts an optional host parameter to send the
// request to. See also example #22 for proper URI parsing.
//
// $ php examples/12-https-client.php
// $ php examples/12-https-client.php reactphp.org
use React\Socket\Connector;
use React\Socket\ConnectionInterface;
$host = isset($argv[1]) ? $argv[1] : 'www.google.com';
require __DIR__ . '/../vendor/autoload.php';
$connector = new Connector();
$connector->connect('tls://' . $host . ':443')->then(function (ConnectionInterface $connection) use ($host) {
$connection->on('data', function ($data) {
echo $data;
});
$connection->on('close', function () {
echo '[CLOSED]' . PHP_EOL;
});
$connection->write("GET / HTTP/1.0\r\nHost: $host\r\n\r\n");
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});