<?php
<?php
/** Uses PHP 5.4 **/
ini_set('display_errors', true);
set_time_limit(0);
require_once __DIR__ . "/vendor/autoload.php";
/** Parse the config into an array **/
$config = (new Symfony\Component\Yaml\Yaml)->parse(__DIR__ . "/config/config.yml");
/** Generate an array of requests **/
$requests = [];
for ($i = 0; $i < $config['num_requests']; $i++)
{
foreach ($config['endpoints'] as $endpoint)
{
$requests[] = $config['base_url'] . $endpoint;
}
}
/** We need to track how many requests remain so we can stop the reactor when finished **/
$requestCountRemaining = count($requests);
// -----------------------------------------------------
/** Create a new HTTP client (abstraction) and reactor (for async awesomeness) **/
$reactor = (new Alert\ReactorFactory)->select();
$client = new Artax\AsyncClient($reactor);
/** Event-driven programming doesn't mean line-by-line any more! **/
$onResponse = function(Artax\Response $response, Artax\Request $request) use (&$requestCountRemaining, $reactor) {
echo $request->getUri(), ' -- ';
echo 'HTTP/', $response->getProtocol(), ' ', $response->getStatus(), ' ', $response->getReason(), "\n";
unset($request);
if (!--$requestCountRemaining)
{
$reactor->stop();
}
};
$onError = function(Exception $e, Artax\Request $request) use (&$requestCountRemaining, $reactor) {
echo $request->getUri(), " failed (", get_class($e), ") : '" . $e->getMessage() . "'\n";
if (!--$requestCountRemaining)
{
$reactor->stop();
}
};
/** Schedule this stuff to happen when the reactor starts **/
$reactor->immediately(function() use ($client, $config, $requests, $onResponse, $onError, $requests) {
$onAuthResponse = function(Artax\Response $response, Artax\Request $request) use ($client, $config, $onResponse, $onError) {
$authTokens = $response->getHeader('WWW-Authenticate')[0];
$explode = explode(',', $authTokens);
$realm = rtrim(str_replace('Digest realm="', '', $explode[0]), '"');
$qop = rtrim(str_replace('qop="', '', $explode[1]), '"');
$nonce = rtrim(str_replace('nonce="', '', $explode[2]), '"');
$uri = $request->getUri();
$cnonce = md5(uniqid());
$nc = 1;
$A1 = md5(sprintf("%s:%s:%s", $config['auth']['username'], $realm, $config['auth']['password']));
$A2 = md5(sprintf("%s:%s", 'GET', $uri));
$hash = md5(sprintf("%s:%s:%s:%s:%s:%s", $A1, $nonce, $nc, $cnonce, $qop, $A2));
$auth = sprintf(
'Digest nonce=%s,nc=%s,cnonce=%s,qop=%s,username=%s,uri=%s,response=%s',
$nonce, $nc, $cnonce, $qop, $config['auth']['username'], $uri, $hash
);
$request = (new Artax\Request)->setMethod('GET')->setUri($uri)->setHeader('Authorization', $auth);
$client->request($request, $onResponse, $onError);
};
foreach ($requests as $key => $uri)
{
$request = (new Artax\Request)->setMethod('GET')->setUri($uri);
$client->request($request, $onAuthResponse, $onError);
}
/** Remember, event-driven. So this will actually happen first.. LOL! **/
echo 'Requesting ', count($requests), ' URIs ...', "\n";
});
/** Start the reactor **/
$reactor->run();
- Output for 5.3.0 - 5.3.28, 5.4.0 - 5.4.29
- Parse error: syntax error, unexpected '<' in /in/UfAKN on line 3
Process exited with code 255.
preferences:
197.79 ms | 1395 KiB | 66 Q