Advertisement
villers

Untitled

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