Skip to content

Minor documentation and example improvement #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,23 @@ Once [installed](#install), you can use the following code to access an
HTTP webserver and send a large number of HTTP GET requests:

```php
<?php

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

$browser = new React\Http\Browser();

// load a huge array of URLs to fetch
$urls = file('urls.txt');

// each job should use the browser to GET a certain URL
// limit number of concurrent jobs here
$q = new Queue(3, null, function ($url) use ($browser) {
$q = new Clue\React\Mq\Queue(3, null, function ($url) use ($browser) {
return $browser->get($url);
});

foreach ($urls as $url) {
$q($url)->then(function (ResponseInterface $response) use ($url) {
$q($url)->then(function (Psr\Http\Message\ResponseInterface $response) use ($url) {
echo $url . ': ' . $response->getBody()->getSize() . ' bytes' . PHP_EOL;
});
}
Expand Down Expand Up @@ -473,7 +477,7 @@ for more details.

## Install

The recommended way to install this library is [through Composer](https://getcomposer.org).
The recommended way to install this library is [through Composer](https://getcomposer.org/).
[New to Composer?](https://getcomposer.org/doc/00-intro.md)

This project follows [SemVer](https://semver.org/).
Expand All @@ -487,12 +491,12 @@ See also the [CHANGELOG](CHANGELOG.md) for details about version upgrades.

This project aims to run on any platform and thus does not require any PHP
extensions and supports running on legacy PHP 5.3 through current PHP 8+.
It's *highly recommended to use PHP 7+* for this project.
It's highly recommended to use the latest supported PHP version for this project.

## Tests

To run the test suite, you first need to clone this repo and then install all
dependencies [through Composer](https://getcomposer.org):
dependencies [through Composer](https://getcomposer.org/):

```bash
$ composer install
Expand All @@ -501,7 +505,7 @@ $ composer install
To run the test suite, go to the project root and run:

```bash
$ php vendor/bin/phpunit
$ vendor/bin/phpunit
```

## License
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@
"clue/block-react": "^1.0",
"phpunit/phpunit": "^9.3 || ^5.7 || ^4.8.35",
"react/event-loop": "^1.2",
"react/http": "^1.4"
"react/http": "^1.5"
}
}
11 changes: 3 additions & 8 deletions examples/01-http.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<?php

use Clue\React\Mq\Queue;
use Psr\Http\Message\ResponseInterface;
use React\EventLoop\Factory;
use React\Http\Browser;

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

// list of all URLs you want to download
Expand All @@ -17,17 +12,17 @@
'http://www.google.com/',
);

$browser = new Browser();
$browser = new React\Http\Browser();

// each job should use the browser to GET a certain URL
// limit number of concurrent jobs here to avoid using excessive network resources
$queue = new Queue(3, null, function ($url) use ($browser) {
$queue = new Clue\React\Mq\Queue(3, null, function ($url) use ($browser) {
return $browser->get($url);
});

foreach ($urls as $url) {
$queue($url)->then(
function (ResponseInterface $response) use ($url) {
function (Psr\Http\Message\ResponseInterface $response) use ($url) {
echo $url . ' has ' . $response->getBody()->getSize() . ' bytes' . PHP_EOL;
},
function (Exception $e) use ($url) {
Expand Down
11 changes: 3 additions & 8 deletions examples/02-http-all.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<?php

use Clue\React\Mq\Queue;
use Psr\Http\Message\ResponseInterface;
use React\EventLoop\Factory;
use React\Http\Browser;

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

// list of all URLs you want to download
Expand All @@ -17,17 +12,17 @@
//'http://httpbin.org/delay/2',
);

$browser = new Browser();
$browser = new React\Http\Browser();

// each job should use the browser to GET a certain URL
// limit number of concurrent jobs here to avoid using excessive network resources
$promise = Queue::all(3, array_combine($urls, $urls), function ($url) use ($browser) {
$promise = Clue\React\Mq\Queue::all(3, array_combine($urls, $urls), function ($url) use ($browser) {
return $browser->get($url);
});

$promise->then(
function ($responses) {
/* @var $responses ResponseInterface[] */
/* @var $responses Psr\Http\Message\ResponseInterface[] */
echo 'All URLs succeeded!' . PHP_EOL;
foreach ($responses as $url => $response) {
echo $url . ' has ' . $response->getBody()->getSize() . ' bytes' . PHP_EOL;
Expand Down
11 changes: 3 additions & 8 deletions examples/03-http-any.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<?php

use Clue\React\Mq\Queue;
use Psr\Http\Message\ResponseInterface;
use React\EventLoop\Factory;
use React\Http\Browser;

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

// list of all URLs you want to try
Expand All @@ -18,13 +13,13 @@
'http://www.google.com/invalid',
);

$browser = new Browser();
$browser = new React\Http\Browser();

// each job should use the browser to GET a certain URL
// limit number of concurrent jobs here to avoid using excessive network resources
$promise = Queue::any(2, $urls, function ($url) use ($browser) {
$promise = Clue\React\Mq\Queue::any(2, $urls, function ($url) use ($browser) {
return $browser->get($url)->then(
function (ResponseInterface $response) use ($url) {
function (Psr\Http\Message\ResponseInterface $response) use ($url) {
// return only the URL for the first successful response
return $url;
}
Expand Down
13 changes: 5 additions & 8 deletions examples/11-http-blocking.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
<?php

use Clue\React\Block;
use Clue\React\Mq\Queue;
use Psr\Http\Message\ResponseInterface;
use React\EventLoop\Factory;
use React\Http\Browser;
use React\EventLoop\Loop;

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

Expand All @@ -20,12 +17,12 @@

function download(array $urls)
{
$browser = new Browser();
$browser = new React\Http\Browser();

$urls = array_combine($urls, $urls);
$promise = Queue::all(3, $urls, function ($url) use ($browser) {
$promise = Clue\React\Mq\Queue::all(3, $urls, function ($url) use ($browser) {
return $browser->get($url)->then(
function (ResponseInterface $response) {
function (Psr\Http\Message\ResponseInterface $response) {
// return only the body for successful responses
return $response->getBody();
},
Expand All @@ -36,7 +33,7 @@ function (Exception $e) {
);
});

return Block\await($promise, $loop);
return Block\await($promise, Loop::get());
}

$responses = download($urls);
Expand Down