1. Go to this page and download the library: Download clue/buzz-react library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
clue / buzz-react example snippets
// old
$browser = new Clue\React\Buzz\Browser($loop);
$browser->get($url);
// new
$browser = new React\Http\Browser($loop);
$browser->get($url);
$browser->get($url)->then(
function (Psr\Http\Message\ResponseInterface $response) {
var_dump('Response received', $response);
},
function (Exception $error) {
var_dump('There was an error', $error->getMessage());
}
);
$promise = $browser->get($url);
$loop->addTimer(2.0, function () use ($promise) {
$promise->cancel();
});
$browser = $browser->withTimeout(10.0);
$browser->get($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
// response received within 10 seconds maximum
var_dump($response->getHeaders());
});
$browser = new Clue\React\Buzz\Browser(
$loop,
new React\Socket\Connector(
$loop,
array(
'timeout' => 5
)
)
);
$browser->get($url, $headers)->then(function (Psr\Http\Message\ResponseInterface $response) {
// the final response will end up here
var_dump($response->getHeaders());
});
$browser = $browser->withFollowRedirects(false);
$browser->get($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
// any redirects will now end up here
var_dump($response->getHeaders());
});
use Clue\React\Block;
$loop = React\EventLoop\Factory::create();
$browser = new Clue\React\Buzz\Browser($loop);
$promise = $browser->get('http://example.com/');
try {
$response = Block\await($promise, $loop);
// response successfully received
} catch (Exception $e) {
// an error occured while performing the request
}
// watch out if array contains many elements
foreach ($urls as $url) {
$browser->get($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
var_dump($response->getHeaders());
});
}
// wraps Browser in a Queue object that executes no more than 10 operations at once
$q = new Clue\React\Mq\Queue(10, null, function ($url) use ($browser) {
return $browser->get($url);
});
foreach ($urls as $url) {
$q($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
var_dump($response->getHeaders());
});
}
$request = new Request('OPTIONS', $url);
// deprecated: see request() instead
$browser->send($request)->then(…);
$browser = $browser->withTimeout(10.0);
$browser = $browser->withTimeout(false);
$browser = $browser->withTimeout(true);
$new = $browser->withFollowRedirects(5);
$browser = $browser->withFollowRedirects(0);
$browser->get($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
// only non-redirected responses will now end up here
var_dump($response->getHeaders());
});
$browser = $browser->withFollowRedirects(false);
$browser->get($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
// any redirects will now end up here
var_dump($response->getHeaderLine('Location'));
});
$browser = $browser->withFollowRedirects(true);
$browser = $browser->withRejectErrorResponse(false);
$browser->get($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
// any HTTP response will now end up here
var_dump($response->getStatusCode(), $response->getReasonPhrase());
});
$browser = $browser->withRejectErrorResponse(true);
$browser->get($url)->then(function (Psr\Http\Message\ResponseInterface $response) {
// any successful HTTP response will now end up here
var_dump($response->getStatusCode(), $response->getReasonPhrase());
}, function (Exception $e) {
if ($e instanceof Clue\React\Buzz\Message\ResponseException) {
// any HTTP response error message will now end up here
$response = $e->getResponse();
var_dump($response->getStatusCode(), $response->getReasonPhrase());
} else {
var_dump($e->getMessage());
}
});
$browser = $browser->withBase('http://api.example.com/v3');
// will request http://api.example.com/v3/example
$browser->get('/example')->then(…);