Advertisement
villers

Untitled

Jan 16th, 2014
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.43 KB | None | 0 0
  1. <?php
  2.  
  3. class CurlMulti
  4. {
  5.     private static $instance;
  6.     protected $multi;
  7.     protected $curl = array();
  8.     protected $done = array();
  9.     public $error = array();
  10.     public $timeout = 1.0;
  11.    
  12.     /** Initialize CURL
  13.     */
  14.     function __construct()
  15.     {
  16.         $this->multi = curl_multi_init();
  17.     }
  18.    
  19.     /** Close CURL
  20.     */
  21.     function __destruct()
  22.     {
  23.         curl_multi_close($this->multi);
  24.     }
  25.  
  26.     public static function getInstance()
  27.     {
  28.         if (!isset(self::$instance))
  29.             self::$instance = new CurlAsync();
  30.  
  31.         return self::$instance;
  32.     }
  33.    
  34.     /** Execute request or get its response
  35.     */
  36.     function __call($name, array $args)
  37.     {
  38.         if ($args) // execute request
  39.         {
  40.             $url = $args[0];
  41.             $header = array();
  42.  
  43.             if(isset($args[1]) && is_array($args[1]))
  44.                 $header = $args[1];
  45.  
  46.             $curl = curl_init($url);
  47.             $this->curl[$name] = $curl;
  48.  
  49.             curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  50.             curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  51.             curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  52.  
  53.             $return = curl_multi_add_handle($this->multi, $curl);
  54.             while (curl_multi_exec($this->multi, $running) == CURLM_CALL_MULTI_PERFORM){}
  55.  
  56.             return $return;
  57.         }
  58.        
  59.         // get response
  60.         if (!isset($this->curl[$name])) // wrong identifier
  61.             return false;
  62.  
  63.         $curl = $this->curl[$name];
  64.        
  65.         while (!isset($this->done[(int) $curl]))
  66.         {
  67.             curl_multi_select($this->multi, $this->timeout);
  68.             while (curl_multi_exec($this->multi, $running) == CURLM_CALL_MULTI_PERFORM){}
  69.             while ($info = curl_multi_info_read($this->multi))
  70.             {
  71.                 if ($info["msg"] == CURLMSG_DONE)
  72.                     $this->done[(int) $info["handle"]] = true;
  73.             }
  74.         }
  75.  
  76.         $this->error[$name] = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  77.  
  78.  
  79.         $body = curl_multi_getcontent($curl);
  80.         $json = json_decode($body);
  81.            
  82.         if (json_last_error() == JSON_ERROR_NONE)
  83.             return $json;
  84.        
  85.         return $body;
  86.     }
  87. }
  88.  
  89.  
  90.  
  91.  
  92. // Utilisation:
  93. $http = CurlMulti::getInstance();
  94.  
  95. $http->test1("https://community-league-of-legends.p.mashape.com/api/v1.0/euw/summoner/retrieveInProgressSpectatorGameInfo/wakazira", array('X-Mashape-Authorization: 90gwEFjQKj5EBjV4kt8mCKhCSKcg7A0s'));
  96. $http->test2("http://prod.api.pvp.net/api/lol/euw/v1.1/summoner/by-name/ghota?api_key=ae41c074-340c-4c93-8a1a-12300ffd135c");
  97.  
  98. var_dump($http->test1());
  99. echo "<br /><br /><br /><br /><br /><br /><br /><br />";
  100. var_dump($http->test2());
  101. echo "<br /><br /><br /><br /><br /><br /><br /><br />";
  102. var_dump($http->error);
  103.  
  104. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement