Skip to content

Commit

Permalink
Add improved HTTP examples
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonFrings committed Sep 3, 2020
1 parent f8fa56f commit 903ac34
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ $browser->get('https://example.com/')->then(function (Psr\Http\Message\ResponseI
});
```

See also [ReactPHP's HTTP client](https://github.com/reactphp/http#client-usage) for more details.
See also [ReactPHP's HTTP client](https://github.com/reactphp/http#client-usage) and any of the [examples](examples) for more details.

#### Connection timeout

Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"require-dev": {
"phpunit/phpunit": "^9.0 || ^7.0 || ^5.0 || ^4.8",
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3",
"react/http": "^1.0",
"clue/block-react": "^1.1"
}
}
33 changes: 33 additions & 0 deletions examples/01-http-request.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

// A simple example which uses an HTTP client to request https://example.com/ through an HTTP CONNECT proxy.
// You can use any kind of proxy, for example https://github.com/leproxy/leproxy and execute it like this:
//
// $ php leproxy-latest.php
//
// To run the example, go to the project root and run:
//
// $ php examples/01-http-requests.php
//
// The proxy can be given as first argument and defaults to localhost:8080 otherwise.
use React\HTTP\Browser;

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

$url = isset($argv[1]) ? $argv[1] : '127.0.0.1:8080';

$loop = React\EventLoop\Factory::create();
$proxy = new Clue\React\HttpProxy\ProxyConnector($url, new React\Socket\Connector($loop));

$connector = new React\Socket\Connector($loop, array(
'tcp' => $proxy,
'dns' => false
));

$browser = new React\Http\Browser($loop, $connector);

$browser->get('https://example.com/')->then(function (Psr\Http\Message\ResponseInterface $response) {
var_dump($response->getHeaders(), (string) $response->getBody());
}, 'printf');

$loop->run();
34 changes: 34 additions & 0 deletions examples/02-optional-proxy-http-request.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

// A simple example which uses an HTTP client to request https://example.com/ (optional: Through an HTTP CONNECT proxy.)
// To run the example, go to the project root and run:
//
// $ php examples/02-optional-proxy-http-request.php
//
// To run the same example with your proxy, the proxy URL can be given as an environment variable:
//
// $ http_proxy=127.0.0.1.8181 php examples/02-optional-proxy-http-request.php

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

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

$connector = null;
$url = getenv('http_proxy');
if ($url !== false) {
$connector = new React\Socket\Connector($loop);
$proxy = new Clue\React\HttpProxy\ProxyConnector($url, $connector);
$connector = new React\Socket\Connector($loop, array(
'tcp' => $proxy,
'timeout' => 3.0,
'dns' => false
));
}

$browser = new React\Http\Browser($loop, $connector);

$browser->get('https://example.com/')->then(function (Psr\Http\Message\ResponseInterface $response) {
var_dump($response->getHeaders(), (string) $response->getBody());
}, 'printf');

$loop->run();
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
<?php

// A simple example which requests https://google.com/ through an HTTP CONNECT proxy.
// You can use any kind of proxy, for example https://github.com/leproxy/leproxy and execute it like this:
//
// $ php leproxy-latest.php
//
// To run the example, go to the project root and run:
//
// $ php examples/11-proxy-raw-https-protocol.php
//
// The proxy can be given as first argument and defaults to localhost:8080 otherwise.
//
// For illustration purposes only. If you want to send HTTP requests in a real
// world project, take a look at https://github.com/clue/reactphp-buzz#http-proxy
// world project, take a look at example #01 and https://github.com/reactphp/http#client-usage.

use Clue\React\HttpProxy\ProxyConnector;
use React\Socket\Connector;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

// A simple example which requests https://google.com/ either directly or through
// an HTTP CONNECT proxy.
// To run the example, go to the project root and run:
//
// $ php examples/12-optional-proxy-raw-https-protocol.php
//
// The Proxy can be given as first argument or does not use a proxy otherwise.
// This example highlights how changing from direct connection to using a proxy
// actually adds very little complexity and does not mess with your actual
// network protocol otherwise.
//
// For illustration purposes only. If you want to send HTTP requests in a real
// world project, take a look at https://github.com/clue/reactphp-buzz#http-proxy
// world project, take a look at example #01 and https://github.com/reactphp/http#client-usage.

use Clue\React\HttpProxy\ProxyConnector;
use React\Socket\Connector;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
<?php

// A simple example which requests https://google.com/ through an HTTP CONNECT proxy.
// You can use any kind of proxy, for example https://github.com/leproxy/leproxy and execute it like this:
//
// $ php leproxy-latest.php
//
// To run the example, go to the project root and run:
//
// $ php examples/13-custom-proxy-headers.php
//
// The proxy can be given as first argument and defaults to localhost:8080 otherwise.
//
// For illustration purposes only. If you want to send HTTP requests in a real
// world project, take a look at https://github.com/clue/reactphp-buzz#http-proxy
// world project, take a look at example #01 and https://github.com/reactphp/http#client-usage.

use Clue\React\HttpProxy\ProxyConnector;
use React\Socket\Connector;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
<?php

// A simple example which uses a plain SMTP connection to Googlemail through a HTTP CONNECT proxy.
// Proxy can be given as first argument and defaults to localhost:8080 otherwise.
// A simple example which requests https://google.com/ through an HTTP CONNECT proxy.
// You can use any kind of proxy, for example https://github.com/leproxy/leproxy and execute it like this:
//
// $ php leproxy-latest.php
//
// To run the example, go to the project root and run:
//
// $ php examples/21-proxy-raw-smtp-protocol.php
//
// The proxy can be given as first argument and defaults to localhost:8080 otherwise.
// Please note that MANY public proxies do not allow SMTP connections, YMMV.

use Clue\React\HttpProxy\ProxyConnector;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
<?php

// A simple example which uses a secure SMTP connection to Googlemail through a HTTP CONNECT proxy.
// Proxy can be given as first argument and defaults to localhost:8080 otherwise.
// A simple example which requests https://google.com/ through an HTTP CONNECT proxy.
// You can use any kind of proxy, for example https://github.com/leproxy/leproxy and execute it like this:
//
// $ php leproxy-latest.php
//
// To run the example, go to the project root and run:
//
// $ php examples/22-proxy-raw-smtps-protocol.php
//
// The proxy can be given as first argument and defaults to localhost:8080 otherwise.
// This example highlights how changing from plain connections (see previous
// example) to using a secure connection actually adds very little complexity
// and does not mess with your actual network protocol otherwise.
Expand Down

0 comments on commit 903ac34

Please sign in to comment.