File tree Expand file tree Collapse file tree 2 files changed +80
-0
lines changed
Expand file tree Collapse file tree 2 files changed +80
-0
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+
3+ // Simple plaintext HTTP client example (for illustration purposes only).
4+ // This shows how a plaintext TCP/IP connection is established to then send an
5+ // application level protocol message (HTTP).
6+ // Real applications should use react/http-client instead!
7+ //
8+ // This simple example only accepts an optional host parameter to send the
9+ // request to.
10+ //
11+ // $ php examples/http.php
12+ // $ php examples/http.php reactphp.org
13+
14+ use React \EventLoop \Factory ;
15+ use React \Stream \DuplexResourceStream ;
16+
17+ require __DIR__ . '/../vendor/autoload.php ' ;
18+
19+ $ host = isset ($ argv [1 ]) ? $ argv [1 ] : 'www.google.com ' ;
20+
21+ // connect to tcp://www.google.com:80 (blocking call!)
22+ // for illustration purposes only, should use react/http-client or react/socket instead!
23+ $ resource = stream_socket_client ('tcp:// ' . $ host . ':80 ' );
24+ if (!$ resource ) {
25+ exit (1 );
26+ }
27+
28+ $ loop = Factory::create ();
29+ $ stream = new DuplexResourceStream ($ resource , $ loop );
30+
31+ $ stream ->on ('data ' , function ($ chunk ) {
32+ echo $ chunk ;
33+ });
34+ $ stream ->on ('close ' , function () {
35+ echo '[CLOSED] ' . PHP_EOL ;
36+ });
37+
38+ $ stream ->write ("GET / HTTP/1.0 \r\nHost: $ host \r\n\r\n" );
39+
40+ $ loop ->run ();
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ // Simple secure HTTPS client example (for illustration purposes only).
4+ // This shows how a secure TLS connection is established to then send an
5+ // application level protocol message (HTTP).
6+ // Real applications should use react/http-client instead!
7+ //
8+ // This simple example only accepts an optional host parameter to send the
9+ // request to.
10+ //
11+ // $ php examples/https.php
12+ // $ php examples/https.php reactphp.org
13+
14+ use React \EventLoop \Factory ;
15+ use React \Stream \DuplexResourceStream ;
16+
17+ require __DIR__ . '/../vendor/autoload.php ' ;
18+
19+ $ host = isset ($ argv [1 ]) ? $ argv [1 ] : 'www.google.com ' ;
20+
21+ // connect to tls://www.google.com:443 (blocking call!)
22+ // for illustration purposes only, should use react/http-client or react/socket instead!
23+ $ resource = stream_socket_client ('tls:// ' . $ host . ':443 ' );
24+ if (!$ resource ) {
25+ exit (1 );
26+ }
27+
28+ $ loop = Factory::create ();
29+ $ stream = new DuplexResourceStream ($ resource , $ loop );
30+
31+ $ stream ->on ('data ' , function ($ chunk ) {
32+ echo $ chunk ;
33+ });
34+ $ stream ->on ('close ' , function () {
35+ echo '[CLOSED] ' . PHP_EOL ;
36+ });
37+
38+ $ stream ->write ("GET / HTTP/1.0 \r\nHost: $ host \r\n\r\n" );
39+
40+ $ loop ->run ();
You can’t perform that action at this time.
0 commit comments