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 Aug 6, 2020
1 parent a5a84ba commit abc6631
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 7 deletions.
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"
}
}
32 changes: 32 additions & 0 deletions examples/01-http-request.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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 excecute it like this:
//
// $ php leproxy-latest.php
//
// To run the example, go to the project root and run:
//
// $ php examples/01-http-requests.php
//
// Make sure you're referencing the right proxy adress inside the ProxyConnector

use React\HTTP\Browser;

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

$loop = React\EventLoop\Factory::create();
$proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080', 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();
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 excecute 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

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

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 excecute 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

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 excecute 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 excecute 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 abc6631

Please sign in to comment.