Guest User

Untitled

a guest
Dec 16th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.82 KB | None | 0 0
  1. <?php
  2. final class curl //version 1.4.
  3. {
  4.     // static
  5.     private static $_chc = array(
  6.         CURLOPT_HEADER => TRUE,
  7.         CURLOPT_RETURNTRANSFER => 1,
  8.         CURLOPT_FOLLOWLOCATION => 1,
  9.         CURLOPT_MAXCONNECTS => 15,
  10.         CURLOPT_CONNECTTIMEOUT => 30,
  11.         CURLOPT_TIMEOUT => 10
  12.     );
  13.     private static $_ch;
  14.     private static $_mh;
  15.     private static $_buf = array();
  16.     private static $_cht = array();
  17.     private static $_chl = 10;
  18.     private static function _init ()
  19.     {
  20.         if(!isset(self::$_ch))
  21.         {
  22.             self::$_ch = curl_init();
  23.             curl_setopt_array(self::$_ch, self::$_chc);
  24.             self::$_mh = curl_multi_init();
  25.         }
  26.     }
  27.     // url cleaner
  28.     static function url ($url, $base = NULL)
  29.     {
  30.         // parse
  31.         $url = parse_url($url);
  32.         $base = parse_url($base);
  33.         $sp = array('scheme','user','pass','host','port');
  34.         // join
  35.         if(!isset($url['scheme']))
  36.         {
  37.             // server parameters
  38.             foreach($sp as $p)
  39.             {
  40.                 if(isset($base[$p]))
  41.                     $url[$p] = $base[$p];
  42.             }
  43.             // path
  44.             if($url['path'][0]!='/')
  45.             {
  46.                 if(!isset($base['path']) || strlen($base['path'])==0)
  47.                     $base['path'] = '/';
  48.                 elseif($base['path'][0]!='/')
  49.                     $base['path'] = '/' . $base['path'];
  50.                 $url['path'] = substr($base['path'], 0, strrpos($base['path'], '/')+1) . $url['path'];
  51.             }
  52.         }
  53.         // reduce
  54.         $url['path'] = preg_replace('#\./#', '/', $url['path']);
  55.         $url['path'] = preg_replace('#/+#', '/', $url['path']);
  56.         do
  57.         {
  58.             $p = $url['path'];
  59.             $url['path'] = preg_replace('#[^/]*[^\.]/\./#', '', $url['path'], 1);
  60.         }
  61.         while($p != $url['path']);
  62.         $url['path'] = preg_replace('#\./#', '', $url['path']);
  63.         // special reduce
  64.        
  65.         // build&return
  66.         return
  67.              ((isset($url['scheme'])) ? $url['scheme'] . '://' : '')
  68.             .((isset($url['user'])) ? $url['user'] . ((isset($url['pass'])) ? ':' . $url['pass'] : '') .'@' : '')
  69.             .((isset($url['host'])) ? $url['host'] : '')
  70.             .((isset($url['port'])) ? ':' . $url['port'] : '')
  71.             .((isset($url['path'])) ? $url['path'] : '')
  72.             .((isset($url['query'])) ? '?' . $url['query'] : '')
  73.         ;
  74.     }
  75.     // single request
  76.     static function get ($url)
  77.     {
  78.         self::_init();
  79.         curl_setopt(self::$_ch, CURLOPT_URL, $url);
  80.         curl_setopt(self::$_ch, CURLOPT_HTTPGET, TRUE);
  81.         $data = curl_exec(self::$_ch);
  82.         return new curl(self::$_ch, $data, $url);
  83.     }
  84.     static function head ($url)
  85.     {
  86.         self::_init();
  87.         curl_setopt(self::$_ch, CURLOPT_URL, $url);
  88.         curl_setopt(self::$_ch, CURLOPT_NOBODY, TRUE);
  89.         $data = curl_exec(self::$_ch);
  90.         return new curl(self::$_ch, $data, $url);
  91.     }
  92.     // multi request
  93.     static function limit ($l = 10)
  94.     {
  95.         $l = intval($l);
  96.         if($l < 1)
  97.             $l = 10;
  98.         if($l > 50)
  99.             $l = 50;
  100.         if($l < 3)
  101.             trigger_error('['. __CLASS__ .'] less than 3 concurring request in inefficent; consider using unparalleled requests', E_USER_WARNING);
  102.         self::$_chl = $l;
  103.     }
  104.     static function queue ($url)
  105.     {
  106.         if(in_array($url, self::$_buf) || in_array($url, self::$_cht))
  107.             return FALSE;
  108.        
  109.         self::$_buf[] = $url;
  110.         return TRUE;
  111.     }
  112.     static function fetch ($timeout = 1)
  113.     {
  114.         self::_init();
  115.         // timeout correction
  116.         $timeout = intval($timeout);
  117.         if($timeout < 1)
  118.             $timeout = 1;
  119.         if($timeout > 10)
  120.             $timeout = 10;
  121.         // add more handles?
  122.         while( count(self::$_cht)<self::$_chl && count(self::$_buf) )
  123.         {
  124.             $url = array_shift(self::$_buf);
  125.             $ch = curl_init();
  126.             curl_setopt_array($ch, self::$_chc);
  127.             curl_setopt($ch, CURLOPT_URL, $url);
  128.             curl_multi_add_handle(self::$_mh, $ch);
  129.             self::$_cht[(int)$ch] = $url;
  130.         }
  131.         // run till timeout
  132.         $to = gettimeofday(true) + $timeout;
  133.         do
  134.         {
  135.             while( curl_multi_exec(self::$_mh, $active)===CURLM_CALL_MULTI_PERFORM ){};
  136.             if($active)
  137.                 curl_multi_select(self::$_mh, $to - gettimeofday(true));
  138.             else
  139.                 usleep(($to - gettimeofday(true)) * 1000000 + 1000);
  140.             if( ($info=curl_multi_info_read(self::$_mh))!==false )
  141.             {
  142.                 // read result
  143.                 $ch = $info["handle"];
  144.                 $url = self::$_cht[(int)$ch];
  145.                 $data = curl_multi_getcontent($ch);
  146.                 $obj = new curl($ch, $data, $url);
  147.                 // continue logic
  148.                 curl_multi_remove_handle(self::$_mh, $ch);
  149.                 if( count(self::$_buf) )
  150.                 {
  151.                     $url = array_shift(self::$_buf);
  152.                     curl_setopt($ch, CURLOPT_URL, $url);
  153.                     curl_multi_add_handle(self::$_mh, $ch);
  154.                     self::$_cht[(int)$ch] = $url;
  155.                 }
  156.                 else
  157.                 {
  158.                     unset(self::$_cht[(int)$ch]);
  159.                     curl_close($ch);
  160.                 }
  161.                 // return object
  162.                 return $obj;
  163.             }
  164.         }
  165.         while( $to > gettimeofday(true) );
  166.         return;
  167.     }
  168.     static function active ()
  169.     {
  170.         return count(self::$_cht);
  171.     }
  172.     static function buffered ()
  173.     {
  174.         return count(self::$_buf);
  175.     }
  176.     // creation
  177.     private function __construct($ch, $data, $url)
  178.     {
  179.         $this->_error = curl_error($ch);
  180.         $this->_errno = curl_errno($ch);
  181.         $p = strpos($data, "\r\n\r\n");
  182.         if($p === FALSE)
  183.             $this->_ret = "";
  184.         else
  185.         {
  186.             $this->_ret = substr($data, $p+4);
  187.             $data = substr($data, 0, $p);
  188.         }
  189.         $data = explode("\r\n", $data);
  190.         array_shift($data);
  191.         $this->_header = array();
  192.         foreach($data as $d)
  193.         {
  194.             $d = trim($d);
  195.             if(strlen($d)==0)
  196.                 continue;
  197.             $p = strpos($d, ": ");
  198.             if($p === FALSE)
  199.             {
  200.                 $k = "";
  201.                 $v = $d;
  202.             }
  203.             else
  204.             {
  205.                 $k = substr($d, 0, strpos($d, ": "));
  206.                 $v = substr($d, strpos($d, ": ")+2);
  207.             }
  208.             if(isset($this->_header[$k]))
  209.             {
  210.                 if(is_array($this->_header[$k]))
  211.                     $this->_header[$k][] = $v;
  212.                 else
  213.                     $this->_header[$k] = array($this->_header[$k], $v);
  214.             }
  215.             else
  216.                 $this->_header[$k] = $v;
  217.         }
  218.         $this->_info = curl_getinfo($ch);
  219.         $this->_info['original_url'] = $url;
  220.     }
  221.     private function __clone() {}
  222.     // dyn
  223.     private $_error;
  224.     private $_errno;
  225.     private $_ret;
  226.     private $_header;
  227.     private $_info;
  228.     function __toString()
  229.     {
  230.         return $this->_ret;
  231.     }
  232.     function __invoke()
  233.     {
  234.         return $this->_ret;
  235.     }
  236.     function size ($p = 0)
  237.     {
  238.         if( $this->failed() )
  239.             return FALSE;
  240.        
  241.         $p = intval($p);
  242.         if($p < 0)
  243.             $p = 0;
  244.         if($p > 3)
  245.             $p = 3;
  246.         return $this->_info['download_content_length']/(1<<(10*$p));
  247.     }
  248.     function info ($type)
  249.     {
  250.         if( $this->failed() )
  251.             return FALSE;
  252.        
  253.         if(is_string($type) && strlen($type) && isset($this->_info[$type]))
  254.             return $this->_info[$type];
  255.         else
  256.             return FALSE;
  257.     }
  258.     function header ($key)
  259.     {
  260.         if( $this->failed() )
  261.             return FALSE;
  262.        
  263.         if(is_string($key) && strlen($key) && isset($this->_header[$key]))
  264.             return $this->_header[$key];
  265.         else
  266.             return "";
  267.     }
  268.     function code ()
  269.     {
  270.         if( $this->failed() )
  271.             return 0;
  272.         else
  273.             return $this->_info['http_code'];
  274.     }
  275.     function ok ()
  276.     {
  277.         return $this->code()==200;
  278.     }
  279.     function failed ()
  280.     {
  281.         if( $this->errno() )
  282.             return TRUE;
  283.         else
  284.             return FALSE;
  285.     }
  286.     function errno ()
  287.     {
  288.         return $this->_errno;
  289.     }
  290.     function error ()
  291.     {
  292.         return $this->_error;
  293.     }
  294.     function dump ()
  295.     {
  296.         print_r($this->_header);
  297.         print_r($this->_info);
  298.         echo $this->error();
  299.     }
  300. }
  301. ?>
Add Comment
Please, Sign In to add comment