1*a1a3b679SAndreas Boehler<?php 2*a1a3b679SAndreas Boehler 3*a1a3b679SAndreas Boehler/** 4*a1a3b679SAndreas Boehler * This example demonstrates the ability for clients to work asynchronously. 5*a1a3b679SAndreas Boehler * 6*a1a3b679SAndreas Boehler * By default up to 10 requests will be executed in paralel. HTTP connections 7*a1a3b679SAndreas Boehler * are re-used and DNS is cached, all thanks to the power of curl. 8*a1a3b679SAndreas Boehler * 9*a1a3b679SAndreas Boehler * @copyright Copyright (C) 2009-2015 fruux GmbH (https://fruux.com/). 10*a1a3b679SAndreas Boehler * @author Evert Pot (http://evertpot.com/) 11*a1a3b679SAndreas Boehler * @license http://sabre.io/license/ Modified BSD License 12*a1a3b679SAndreas Boehler */ 13*a1a3b679SAndreas Boehleruse Sabre\HTTP\Request; 14*a1a3b679SAndreas Boehleruse Sabre\HTTP\Client; 15*a1a3b679SAndreas Boehler 16*a1a3b679SAndreas Boehler// Find the autoloader 17*a1a3b679SAndreas Boehler$paths = [ 18*a1a3b679SAndreas Boehler __DIR__ . '/../vendor/autoload.php', 19*a1a3b679SAndreas Boehler __DIR__ . '/../../../autoload.php', 20*a1a3b679SAndreas Boehler __DIR__ . '/vendor/autoload.php', 21*a1a3b679SAndreas Boehler 22*a1a3b679SAndreas Boehler]; 23*a1a3b679SAndreas Boehler 24*a1a3b679SAndreas Boehlerforeach ($paths as $path) { 25*a1a3b679SAndreas Boehler if (file_exists($path)) { 26*a1a3b679SAndreas Boehler include $path; 27*a1a3b679SAndreas Boehler break; 28*a1a3b679SAndreas Boehler } 29*a1a3b679SAndreas Boehler} 30*a1a3b679SAndreas Boehler 31*a1a3b679SAndreas Boehler// This is the request we're repeating a 1000 times. 32*a1a3b679SAndreas Boehler$request = new Request('GET', 'http://localhost/'); 33*a1a3b679SAndreas Boehler$client = new Client(); 34*a1a3b679SAndreas Boehler 35*a1a3b679SAndreas Boehlerfor ($i = 0; $i < 1000; $i++) { 36*a1a3b679SAndreas Boehler 37*a1a3b679SAndreas Boehler echo "$i sending\n"; 38*a1a3b679SAndreas Boehler $client->sendAsync( 39*a1a3b679SAndreas Boehler $request, 40*a1a3b679SAndreas Boehler 41*a1a3b679SAndreas Boehler // This is the 'success' callback 42*a1a3b679SAndreas Boehler function($response) use ($i) { 43*a1a3b679SAndreas Boehler echo "$i -> " . $response->getStatus() . "\n"; 44*a1a3b679SAndreas Boehler }, 45*a1a3b679SAndreas Boehler 46*a1a3b679SAndreas Boehler // This is the 'error' callback. It is called for general connection 47*a1a3b679SAndreas Boehler // problems (such as not being able to connect to a host, dns errors, 48*a1a3b679SAndreas Boehler // etc.) and also cases where a response was returned, but it had a 49*a1a3b679SAndreas Boehler // status code of 400 or higher. 50*a1a3b679SAndreas Boehler function($error) use ($i) { 51*a1a3b679SAndreas Boehler 52*a1a3b679SAndreas Boehler if ($error['status'] === Client::STATUS_CURLERROR) { 53*a1a3b679SAndreas Boehler // Curl errors 54*a1a3b679SAndreas Boehler echo "$i -> curl error: " . $error['curl_errmsg'] . "\n"; 55*a1a3b679SAndreas Boehler } else { 56*a1a3b679SAndreas Boehler // HTTP errors 57*a1a3b679SAndreas Boehler echo "$i -> " . $error['response']->getStatus() . "\n"; 58*a1a3b679SAndreas Boehler } 59*a1a3b679SAndreas Boehler } 60*a1a3b679SAndreas Boehler ); 61*a1a3b679SAndreas Boehler} 62*a1a3b679SAndreas Boehler 63*a1a3b679SAndreas Boehler// After everything is done, we call 'wait'. This causes the client to wait for 64*a1a3b679SAndreas Boehler// all outstanding http requests to complete. 65*a1a3b679SAndreas Boehler$client->wait(); 66