├── CurlAsync.php ├── MysqliAsync.php ├── readme.txt └── tests ├── curl-basic.php ├── db.inc.php ├── mysqli-basic.php ├── mysqli-sleep.php └── mysqli-wrong.php /CurlAsync.php: -------------------------------------------------------------------------------- 1 | multi = curl_multi_init(); 19 | } 20 | 21 | /** Close CURL 22 | */ 23 | function __destruct() { 24 | curl_multi_close($this->multi); 25 | } 26 | 27 | /** Execute request or get its response 28 | * @param string request identifier 29 | * @param array array(string $url) for executing request, array() for getting response 30 | * @return mixed 31 | */ 32 | function __call($name, array $args) { 33 | if ($args) { // execute request 34 | list($url) = $args; 35 | $curl = curl_init($url); 36 | $this->curl[$name] = $curl; 37 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 38 | $return = curl_multi_add_handle($this->multi, $curl); 39 | while (curl_multi_exec($this->multi, $running) == CURLM_CALL_MULTI_PERFORM) { 40 | } 41 | return $return; 42 | } 43 | 44 | // get response 45 | if (!isset($this->curl[$name])) { // wrong identifier 46 | return false; 47 | } 48 | $curl = $this->curl[$name]; 49 | 50 | while (!isset($this->done[(int) $curl])) { 51 | curl_multi_select($this->multi, $this->timeout); 52 | while (curl_multi_exec($this->multi, $running) == CURLM_CALL_MULTI_PERFORM) { 53 | } 54 | while ($info = curl_multi_info_read($this->multi)) { 55 | if ($info["msg"] == CURLMSG_DONE) { 56 | $this->done[(int) $info["handle"]] = true; 57 | } 58 | } 59 | } 60 | 61 | return curl_multi_getcontent($curl); 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /MysqliAsync.php: -------------------------------------------------------------------------------- 1 | credentials = func_get_args(); 19 | } 20 | 21 | /** Close all opened connections 22 | */ 23 | function __destruct() { 24 | foreach ($this->connections as $connection) { 25 | $connection->close(); 26 | } 27 | } 28 | 29 | /** Execute query or get its data 30 | * @param string query identifier 31 | * @param array array(string $query, [array $credentials]) for executing query, array() for getting data 32 | * @return bool|mysqli_result 33 | */ 34 | function __call($name, array $args) { 35 | if ($args) { // execute query 36 | $query = $args[0]; 37 | $credentials = $this->credentials; 38 | if (isset($args[1])) { 39 | $credentials = $args[1] + $credentials; 40 | } 41 | $connection = call_user_func_array('mysqli_connect', $credentials); 42 | $this->connections[$name] = $connection; 43 | return $connection->query($query, MYSQLI_ASYNC); 44 | } 45 | 46 | // get data 47 | if (!isset($this->connections[$name])) { // wrong identifier 48 | return false; 49 | } 50 | //! handle second call with the same $name 51 | $connection = $this->connections[$name]; 52 | 53 | do { 54 | $links = $errors = $reject = $this->connections; 55 | mysqli_poll($links, $errors, $reject, $this->timeout); 56 | } while (!in_array($connection, $links, true) && !in_array($connection, $errors, true) && !in_array($connection, $reject, true)); 57 | 58 | return $connection->reap_async_query(); 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /readme.txt: -------------------------------------------------------------------------------- 1 | MysqliAsync - Convenient API for asynchronous queries in PHP 5.3+ 2 | CurlAsync - Convenient API for asynchronous HTTP connections using CURL in PHP 5+ 3 | 4 | 5 | 6 | MysqliAsync API: 7 | 8 | $db = new MysqliAsync([$host, [$username, [$passwd, [$dbname, [$port, [$socket]]]]]]) 9 | - specify default credentials 10 | 11 | $db->$name($query, [$credentials]) 12 | - execute query 13 | - $credentials are merged with defaults 14 | 15 | $mysqli_result = $db->$name() 16 | - get results of query identified by $name 17 | 18 | 19 | 20 | CurlAsync API: 21 | 22 | $http = new CurlAsync 23 | 24 | $http->$name($url) 25 | - execute request 26 | 27 | $string = $http->$name() 28 | - get result of request identified by $name 29 | 30 | 31 | 32 | Czech articles: 33 | 34 | http://php.vrana.cz/asynchronni-dotazy-v-mysqli.php 35 | http://php.vrana.cz/asynchronni-dotazy-v-curl.php 36 | -------------------------------------------------------------------------------- /tests/curl-basic.php: -------------------------------------------------------------------------------- 1 | test1("http://localhost/"); 7 | $http->test2("http://localhost/"); 8 | 9 | var_dump($http->test1()); 10 | var_dump($http->test2()); 11 | -------------------------------------------------------------------------------- /tests/db.inc.php: -------------------------------------------------------------------------------- 1 | test1("SELECT 'test1'"); 5 | $db->test2("SELECT 'test2'"); 6 | 7 | print_r($db->test1()->fetch_row()); 8 | print_r($db->test2()->fetch_row()); 9 | -------------------------------------------------------------------------------- /tests/mysqli-sleep.php: -------------------------------------------------------------------------------- 1 | a("SELECT SLEEP(2)"); 7 | $db->b("SELECT SLEEP(2)"); 8 | 9 | sleep(2); 10 | 11 | $db->a(); 12 | $db->b(); 13 | 14 | echo (microtime(true) - $start) . "\n"; // should be slightly more than 2 15 | -------------------------------------------------------------------------------- /tests/mysqli-wrong.php: -------------------------------------------------------------------------------- 1 | wrong()); 5 | --------------------------------------------------------------------------------