ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
asyncclient.php
Go to the documentation of this file.
1 <?php
2 
15 
16 // Find the autoloader
17 $paths = [
18  __DIR__ . '/../vendor/autoload.php',
19  __DIR__ . '/../../../autoload.php',
20  __DIR__ . '/vendor/autoload.php',
21 
22 ];
23 
24 foreach ($paths as $path) {
25  if (file_exists($path)) {
26  include $path;
27  break;
28  }
29 }
30 
31 // This is the request we're repeating a 1000 times.
32 $request = new Request('GET', 'http://localhost/');
33 $client = new Client();
34 
35 for ($i = 0; $i < 1000; $i++) {
36 
37  echo "$i sending\n";
38  $client->sendAsync(
39  $request,
40 
41  // This is the 'success' callback
42  function($response) use ($i) {
43  echo "$i -> " . $response->getStatus() . "\n";
44  },
45 
46  // This is the 'error' callback. It is called for general connection
47  // problems (such as not being able to connect to a host, dns errors,
48  // etc.) and also cases where a response was returned, but it had a
49  // status code of 400 or higher.
50  function($error) use ($i) {
51 
52  if ($error['status'] === Client::STATUS_CURLERROR) {
53  // Curl errors
54  echo "$i -> curl error: " . $error['curl_errmsg'] . "\n";
55  } else {
56  // HTTP errors
57  echo "$i -> " . $error['response']->getStatus() . "\n";
58  }
59  }
60  );
61 }
62 
63 // After everything is done, we call 'wait'. This causes the client to wait for
64 // all outstanding http requests to complete.
65 $client->wait();
$path
Definition: aliased.php:25
foreach($paths as $path) $request
Definition: asyncclient.php:32
The Request class represents a single HTTP request.
Definition: Request.php:18
$client
Definition: asyncclient.php:33
$paths
Definition: asyncclient.php:17
This example demonstrates the ability for clients to work asynchronously.
$i
Definition: disco.tpl.php:19
$response
A rudimentary HTTP client.
Definition: Client.php:44