Skip to content

Commit aaf8094

Browse files
authored
Merge pull request #35 from SimonFrings/readme
Update README to reactphp/http v1.0.0 and add improved HTTP examples
2 parents 2404c69 + c12e37f commit aaf8094

9 files changed

+187
-25
lines changed

README.md

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,29 @@ $connector->connect('tls://smtp.googlemail.com:465')->then(function (ConnectionI
203203

204204
#### HTTP requests
205205

206-
HTTP operates on a higher layer than this low-level HTTP CONNECT implementation.
207-
If you want to issue HTTP requests, you can add a dependency for
208-
[clue/reactphp-buzz](https://github.com/clue/reactphp-buzz).
209-
It can interact with this library by issuing all
210-
[HTTP requests through a HTTP CONNECT proxy server](https://github.com/clue/reactphp-buzz#http-proxy).
211-
This works for both plain HTTP and TLS-encrypted HTTPS requests.
206+
This library also allows you to send HTTP requests through an HTTP CONNECT proxy server.
207+
208+
In order to send HTTP requests, you first have to add a dependency for [ReactPHP's async HTTP client](https://github.com/reactphp/http#client-usage). This allows you to send both plain HTTP and TLS-encrypted HTTPS requests like this:
209+
210+
```php
211+
$proxy = new Clue\React\HttpProxy\ProxyConnector(
212+
'http://127.0.0.1:8080',
213+
new React\Socket\Connector($loop)
214+
);
215+
216+
$connector = new React\Socket\Connector($loop, array(
217+
'tcp' => $proxy,
218+
'dns' => false
219+
));
220+
221+
$browser = new React\Http\Browser($loop, $connector);
222+
223+
$browser->get('https://example.com/')->then(function (Psr\Http\Message\ResponseInterface $response) {
224+
var_dump($response->getHeaders(), (string) $response->getBody());
225+
});
226+
```
227+
228+
See also [ReactPHP's HTTP client](https://github.com/reactphp/http#client-usage) and any of the [examples](examples) for more details.
212229

213230
#### Connection timeout
214231

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"require-dev": {
2626
"phpunit/phpunit": "^9.0 || ^7.0 || ^5.0 || ^4.8",
2727
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3",
28+
"react/http": "^1.0",
2829
"clue/block-react": "^1.1"
2930
}
3031
}

examples/01-http-request.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
// A simple example which uses an HTTP client to request https://example.com/ through an HTTP CONNECT proxy.
4+
// You can use any kind of proxy, for example https://github.com/leproxy/leproxy and execute it like this:
5+
//
6+
// $ php leproxy.php
7+
//
8+
// The proxy defaults to localhost:8080.
9+
// To run the example go to the project root and run:
10+
//
11+
// $ php examples/01-http-requests.php
12+
//
13+
// To run the same example with your proxy, the proxy URL can be given as an environment variable:
14+
//
15+
// $ http_proxy=127.0.0.2:8080 php examples/01-http-requests.php
16+
17+
require __DIR__ . '/../vendor/autoload.php';
18+
19+
$url = getenv('http_proxy');
20+
if ($url === false) {
21+
$url = 'localhost:8080';
22+
}
23+
24+
$loop = React\EventLoop\Factory::create();
25+
$proxy = new Clue\React\HttpProxy\ProxyConnector($url, new React\Socket\Connector($loop));
26+
27+
$connector = new React\Socket\Connector($loop, array(
28+
'tcp' => $proxy,
29+
'dns' => false
30+
));
31+
32+
$browser = new React\Http\Browser($loop, $connector);
33+
34+
$browser->get('https://example.com/')->then(function (Psr\Http\Message\ResponseInterface $response) {
35+
var_dump($response->getHeaders(), (string) $response->getBody());
36+
}, 'printf');
37+
38+
$loop->run();
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
// A simple example which uses an HTTP client to request https://example.com/ (optional: Through an HTTP CONNECT proxy.)
4+
// To run the example, go to the project root and run:
5+
//
6+
// $ php examples/02-optional-proxy-http-request.php
7+
//
8+
// If you chose the optional route, you can use any kind of proxy, for example https://github.com/leproxy/leproxy and execute it like this:
9+
//
10+
// $ php leproxy.php
11+
//
12+
// To run the same example with your proxy, the proxy URL can be given as an environment variable:
13+
//
14+
// $ http_proxy=127.0.0.2:8080 php examples/02-optional-proxy-http-request.php
15+
16+
require __DIR__ . '/../vendor/autoload.php';
17+
18+
$loop = React\EventLoop\Factory::create();
19+
20+
$connector = null;
21+
$url = getenv('http_proxy');
22+
if ($url !== false) {
23+
$connector = new React\Socket\Connector($loop);
24+
$proxy = new Clue\React\HttpProxy\ProxyConnector($url, $connector);
25+
$connector = new React\Socket\Connector($loop, array(
26+
'tcp' => $proxy,
27+
'timeout' => 3.0,
28+
'dns' => false
29+
));
30+
}
31+
32+
$browser = new React\Http\Browser($loop, $connector);
33+
34+
$browser->get('https://example.com/')->then(function (Psr\Http\Message\ResponseInterface $response) {
35+
var_dump($response->getHeaders(), (string) $response->getBody());
36+
}, 'printf');
37+
38+
$loop->run();

examples/01-proxy-https.php renamed to examples/11-proxy-raw-https-protocol.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,32 @@
11
<?php
22

33
// A simple example which requests https://google.com/ through an HTTP CONNECT proxy.
4-
// The proxy can be given as first argument and defaults to localhost:8080 otherwise.
4+
// You can use any kind of proxy, for example https://github.com/leproxy/leproxy and execute it like this:
5+
//
6+
// $ php leproxy.php
7+
//
8+
// The proxy defaults to localhost:8080.
9+
// To run the example, go to the project root and run:
10+
//
11+
// $ php examples/11-proxy-raw-https-protocol.php
12+
//
13+
// To run the same example with your proxy, the proxy URL can be given as an environment variable:
14+
//
15+
// $ http_proxy=127.0.0.2:8080 php examples/11-proxy-raw-https-protocol.php
516
//
617
// For illustration purposes only. If you want to send HTTP requests in a real
7-
// world project, take a look at https://github.com/clue/reactphp-buzz#http-proxy
18+
// world project, take a look at example #01, example #02 and https://github.com/reactphp/http#client-usage.
819

920
use Clue\React\HttpProxy\ProxyConnector;
1021
use React\Socket\Connector;
1122
use React\Socket\ConnectionInterface;
1223

1324
require __DIR__ . '/../vendor/autoload.php';
1425

15-
$url = isset($argv[1]) ? $argv[1] : '127.0.0.1:8080';
26+
$url = getenv('http_proxy');
27+
if ($url === false) {
28+
$url = 'localhost:8080';
29+
}
1630

1731
$loop = React\EventLoop\Factory::create();
1832

examples/02-optional-proxy-https.php renamed to examples/12-optional-proxy-raw-https-protocol.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
<?php
22

3-
// A simple example which requests https://google.com/ either directly or through
4-
// an HTTP CONNECT proxy.
5-
// The Proxy can be given as first argument or does not use a proxy otherwise.
3+
// A simple example which requests https://google.com/ directly (optional: Through an HTTP CONNECT proxy.)
4+
// To run the example, go to the project root and run:
5+
//
6+
// $ php examples/12-optional-proxy-raw-https-protocol.php
7+
//
8+
// If you chose the optional route, you can use any kind of proxy, for example https://github.com/leproxy/leproxy and execute it like this:
9+
//
10+
// $ php leproxy.php
11+
//
12+
// To run the same example with your proxy, the proxy URL can be given as an environment variable:
13+
//
14+
// $ http_proxy=127.0.0.2:8080 php examples/12-optional-proxy-raw-https-protocol.php
15+
//
616
// This example highlights how changing from direct connection to using a proxy
717
// actually adds very little complexity and does not mess with your actual
818
// network protocol otherwise.
919
//
1020
// For illustration purposes only. If you want to send HTTP requests in a real
11-
// world project, take a look at https://github.com/clue/reactphp-buzz#http-proxy
21+
// world project, take a look at example #01, example #02 and https://github.com/reactphp/http#client-usage.
1222

1323
use Clue\React\HttpProxy\ProxyConnector;
1424
use React\Socket\Connector;
@@ -20,9 +30,9 @@
2030

2131
$connector = new Connector($loop);
2232

23-
// first argument given? use this as the proxy URL
24-
if (isset($argv[1])) {
25-
$proxy = new ProxyConnector($argv[1], $connector);
33+
$url = getenv('http_proxy');
34+
if ($url !== false) {
35+
$proxy = new ProxyConnector($url, $connector);
2636
$connector = new Connector($loop, array(
2737
'tcp' => $proxy,
2838
'timeout' => 3.0,

examples/03-custom-proxy-headers.php renamed to examples/13-custom-proxy-headers.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,32 @@
11
<?php
22

33
// A simple example which requests https://google.com/ through an HTTP CONNECT proxy.
4-
// The proxy can be given as first argument and defaults to localhost:8080 otherwise.
4+
// You can use any kind of proxy, for example https://github.com/leproxy/leproxy and execute it like this:
5+
//
6+
// $ php leproxy.php
7+
//
8+
// The proxy defaults to localhost:8080.
9+
// To run the example, go to the project root and run:
10+
//
11+
// $ php examples/13-custom-proxy-headers.php
12+
//
13+
// To run the same example with your proxy, the proxy URL can be given as an environment variable:
14+
//
15+
// $ http_proxy=127.0.0.2:8080 php examples/13-custom-proxy-headers.php
516
//
617
// For illustration purposes only. If you want to send HTTP requests in a real
7-
// world project, take a look at https://github.com/clue/reactphp-buzz#http-proxy
18+
// world project, take a look at example #01, example #02 and https://github.com/reactphp/http#client-usage.
819

920
use Clue\React\HttpProxy\ProxyConnector;
1021
use React\Socket\Connector;
1122
use React\Socket\ConnectionInterface;
1223

1324
require __DIR__ . '/../vendor/autoload.php';
1425

15-
$url = isset($argv[1]) ? $argv[1] : '127.0.0.1:8080';
26+
$url = getenv('http_proxy');
27+
if ($url === false) {
28+
$url = 'localhost:8080';
29+
}
1630

1731
$loop = React\EventLoop\Factory::create();
1832

examples/11-proxy-smtp.php renamed to examples/21-proxy-raw-smtp-protocol.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
<?php
22

3-
// A simple example which uses a plain SMTP connection to Googlemail through a HTTP CONNECT proxy.
4-
// Proxy can be given as first argument and defaults to localhost:8080 otherwise.
3+
// A simple example which requests https://google.com/ through an HTTP CONNECT proxy.
4+
// You can use any kind of proxy, for example https://github.com/leproxy/leproxy and execute it like this:
5+
//
6+
// $ php leproxy.php
7+
//
8+
// The proxy defaults to localhost:8080.
9+
// To run the example, go to the project root and run:
10+
//
11+
// $ php examples/21-proxy-raw-smtp-protocol.php
12+
//
13+
// To run the same example with your proxy, the proxy URL can be given as an environment variable:
14+
//
15+
// $ http_proxy=127.0.0.2:8080 php examples/21-proxy-raw-smtp-protocol.php
16+
//
517
// Please note that MANY public proxies do not allow SMTP connections, YMMV.
618

719
use Clue\React\HttpProxy\ProxyConnector;
@@ -10,7 +22,10 @@
1022

1123
require __DIR__ . '/../vendor/autoload.php';
1224

13-
$url = isset($argv[1]) ? $argv[1] : '127.0.0.1:8080';
25+
$url = getenv('http_proxy');
26+
if ($url === false) {
27+
$url = 'localhost:8080';
28+
}
1429

1530
$loop = React\EventLoop\Factory::create();
1631

examples/12-proxy-smtps.php renamed to examples/22-proxy-raw-smtps-protocol.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
<?php
22

3-
// A simple example which uses a secure SMTP connection to Googlemail through a HTTP CONNECT proxy.
4-
// Proxy can be given as first argument and defaults to localhost:8080 otherwise.
3+
// A simple example which requests https://google.com/ through an HTTP CONNECT proxy.
4+
// You can use any kind of proxy, for example https://github.com/leproxy/leproxy and execute it like this:
5+
//
6+
// $ php leproxy.php
7+
//
8+
// The proxy defaults to localhost:8080.
9+
// To run the example, go to the project root and run:
10+
//
11+
// $ php examples/22-proxy-raw-smtps-protocol.php
12+
//
13+
// To run the same example with your proxy, the proxy URL can be given as an environment variable:
14+
//
15+
// $ http_proxy=127.0.0.2:8080 php examples/22-proxy-raw-smtps-protocol.php
16+
//
517
// This example highlights how changing from plain connections (see previous
618
// example) to using a secure connection actually adds very little complexity
719
// and does not mess with your actual network protocol otherwise.
@@ -13,7 +25,10 @@
1325

1426
require __DIR__ . '/../vendor/autoload.php';
1527

16-
$url = isset($argv[1]) ? $argv[1] : '127.0.0.1:8080';
28+
$url = getenv('http_proxy');
29+
if ($url === false) {
30+
$url = 'localhost:8080';
31+
}
1732

1833
$loop = React\EventLoop\Factory::create();
1934

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy