forked from vrana/php-async
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCurlAsync.php
64 lines (57 loc) · 1.68 KB
/
CurlAsync.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
/** Convenient API for asynchronous HTTP connections using CURL in PHP 5+
* @link http://github.com/vrana/curl-async
* @author Jakub Vrana, http://www.vrana.cz/
* @copyright 2010 Jakub Vrana
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
*/
class CurlAsync {
/** @var float number of seconds to wait in retrieving data */
public $timeout = 1.0;
protected $multi;
protected $curl = array();
protected $done = array();
/** Initialize CURL
*/
function __construct() {
$this->multi = curl_multi_init();
}
/** Close CURL
*/
function __destruct() {
curl_multi_close($this->multi);
}
/** Execute request or get its response
* @param string request identifier
* @param array array(string $url) for executing request, array() for getting response
* @return mixed
*/
function __call($name, array $args) {
if ($args) { // execute request
list($url) = $args;
$curl = curl_init($url);
$this->curl[$name] = $curl;
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$return = curl_multi_add_handle($this->multi, $curl);
while (curl_multi_exec($this->multi, $running) == CURLM_CALL_MULTI_PERFORM) {
}
return $return;
}
// get response
if (!isset($this->curl[$name])) { // wrong identifier
return false;
}
$curl = $this->curl[$name];
while (!isset($this->done[(int) $curl])) {
curl_multi_select($this->multi, $this->timeout);
while (curl_multi_exec($this->multi, $running) == CURLM_CALL_MULTI_PERFORM) {
}
while ($info = curl_multi_info_read($this->multi)) {
if ($info["msg"] == CURLMSG_DONE) {
$this->done[(int) $info["handle"]] = true;
}
}
}
return curl_multi_getcontent($curl);
}
}