Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class CurlMulti
- {
- private static $instance;
- protected $multi;
- protected $curl = array();
- protected $done = array();
- public $error = array();
- public $timeout = 1.0;
- /** Initialize CURL
- */
- function __construct()
- {
- $this->multi = curl_multi_init();
- }
- /** Close CURL
- */
- function __destruct()
- {
- curl_multi_close($this->multi);
- }
- public static function getInstance()
- {
- if (!isset(self::$instance))
- self::$instance = new CurlAsync();
- return self::$instance;
- }
- /** Execute request or get its response
- */
- function __call($name, array $args)
- {
- if ($args) // execute request
- {
- $url = $args[0];
- $header = array();
- if(isset($args[1]) && is_array($args[1]))
- $header = $args[1];
- $curl = curl_init($url);
- $this->curl[$name] = $curl;
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
- $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;
- }
- }
- $this->error[$name] = curl_getinfo($curl, CURLINFO_HTTP_CODE);
- $body = curl_multi_getcontent($curl);
- $json = json_decode($body);
- if (json_last_error() == JSON_ERROR_NONE)
- return $json;
- return $body;
- }
- }
- // Utilisation:
- $http = CurlMulti::getInstance();
- $http->test1("https://community-league-of-legends.p.mashape.com/api/v1.0/euw/summoner/retrieveInProgressSpectatorGameInfo/wakazira", array('X-Mashape-Authorization: 90gwEFjQKj5EBjV4kt8mCKhCSKcg7A0s'));
- $http->test2("http://prod.api.pvp.net/api/lol/euw/v1.1/summoner/by-name/ghota?api_key=ae41c074-340c-4c93-8a1a-12300ffd135c");
- var_dump($http->test1());
- echo "<br /><br /><br /><br /><br /><br /><br /><br />";
- var_dump($http->test2());
- echo "<br /><br /><br /><br /><br /><br /><br /><br />";
- var_dump($http->error);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement