Advertisement
Guest User

Untitled

a guest
Oct 1st, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.40 KB | None | 0 0
  1. <?php
  2.     /**
  3.      * http://code.google.com/p/gam-http/
  4.      */
  5.  
  6.     class Http_Exception extends Exception {
  7.         const NOT_MODIFIED = 304;
  8.         const BAD_REQUEST = 400;
  9.         const NOT_FOUND = 404;
  10.         const NOT_ALOWED = 405;
  11.         const CONFLICT = 409;
  12.         const PRECONDITION_FAILED = 412;
  13.         const INTERNAL_ERROR = 500;
  14.     }
  15.    
  16.     class Http_Multiple_Error {
  17.         private $_status = null;
  18.         private $_type = null;
  19.         private $_url = null;
  20.         private $_params = null;
  21.        
  22.         function __construct($status, $type, $url, $params) {
  23.             $this->_status = $status;
  24.             $this->_type = $type;
  25.             $this->_url = $url;
  26.             $this->_params = $params;
  27.         }
  28.        
  29.         function getStatus() {
  30.             return $this->_status;
  31.         }
  32.        
  33.         function getType() {
  34.             return $this->_type;
  35.         }
  36.        
  37.         function getUrl() {
  38.             return $this->_url;
  39.         }
  40.        
  41.         function getParams() {
  42.             return $this->_params;
  43.         }
  44.     }
  45.    
  46.     class Http {
  47.         private $_host = null;
  48.         private $_port = null;
  49.         private $_user = null;
  50.         private $_pass = null;
  51.         private $_protocol = null;
  52.         private $_timeout = 0;
  53.         private $_connectTimeout = 0;
  54.    
  55.         const HTTP = 'http';
  56.         const HTTPS = 'https';
  57.        
  58.         private $_connMultiple = false;
  59.        
  60.         /**
  61.          * Factory of the class. Lazy connect
  62.          *
  63.          * @param string $host
  64.          * @param integer $port
  65.          * @param string $user
  66.          * @param string $pass
  67.          * @return Http
  68.          */
  69.         static public function connect($host, $port = 80, $protocol = self::HTTP) {
  70.             return new self($host, $port, $protocol, false);
  71.         }
  72.        
  73.         /**
  74.          *
  75.          * @return Http
  76.          */
  77.         static public function multiConnect() {
  78.             return new self(null, null, null, true);
  79.         }
  80.    
  81.         private $_append = array();
  82.        
  83.         public function add($http) {
  84.             $this->_append[] = $http;
  85.             return $this;
  86.         }
  87.        
  88.         private $_silentMode = false;
  89.        
  90.         /**
  91.          *
  92.          * @param bool $mode
  93.          * @return Http
  94.          */
  95.         public function silentMode($mode=true) {
  96.             $this->_silentMode = $mode;
  97.             return $this;    
  98.         }
  99.        
  100.         protected function __construct($host, $port, $protocol, $connMultiple) {
  101.             $this->_connMultiple = $connMultiple;
  102.            
  103.             $this->_host = $host;
  104.             $this->_port = $port;
  105.             $this->_protocol = $protocol;
  106.         }
  107.        
  108.         public function setCredentials($user, $pass) {
  109.             $this->_user = $user;
  110.             $this->_pass = $pass;
  111.             return $this;
  112.         }
  113.        
  114.         public function setTimeout($timeout) {
  115.             $this->_timeout = $timeout;
  116.         }
  117.        
  118.         public function getTimeout() {
  119.             return $this->_timeout;
  120.         }
  121.        
  122.         public function setConnectTimeout($connectTimeout) {
  123.             $this->_connectTimeout = $connectTimeout;
  124.         }
  125.        
  126.         public function getConnectTimeout() {
  127.             return $this->_connectTimeout;
  128.         }
  129.    
  130.         const POST = 'POST';
  131.         const GET = 'GET';
  132.         const DELETE = 'DELETE';
  133.         const PUT = 'PUT';
  134.    
  135.         private $_requests = array();
  136.        
  137.         /**
  138.          * @param string $url
  139.          * @param array $params
  140.          * @return Http
  141.          */
  142.         public function put($url, $params=array()) {
  143.             $this->_requests[] = array(self::PUT, $this->_url($url), $params);
  144.             return $this;
  145.         }
  146.        
  147.         /**
  148.          * @param string $url
  149.          * @param array $params
  150.          * @return Http
  151.          */
  152.         public function post($url, $params=array()) {
  153.             $this->_requests[] = array(self::POST, $this->_url($url), $params);
  154.             return $this;
  155.         }
  156.    
  157.         /**
  158.          * @param string $url
  159.          * @param array $params
  160.          * @return Http
  161.          */
  162.         public function get($url, $params=array()) {
  163.             $this->_requests[] = array(self::GET, $this->_url($url), $params);
  164.             return $this;
  165.         }
  166.        
  167.         /**
  168.          * @param string $url
  169.          * @param array $params
  170.          * @return Http
  171.          */
  172.         public function delete($url, $params=array()) {
  173.             $this->_requests[] = array(self::DELETE, $this->_url($url), $params);
  174.             return $this;
  175.         }
  176.        
  177.         public function _getRequests() {
  178.             return $this->_requests;
  179.         }
  180.        
  181.         /**
  182.          * PUT request
  183.          *
  184.          * @param string $url
  185.          * @param array $params
  186.          * @return string
  187.          */
  188.         public function doPut($url, $params=array()) {
  189.             return $this->_exec(self::PUT, $this->_url($url), $params);
  190.         }
  191.        
  192.         /**
  193.          * POST request
  194.          *
  195.          * @param string $url
  196.          * @param array $params
  197.          * @return string
  198.          */
  199.         public function doPost($url, $params=array()) {
  200.             return $this->_exec(self::POST, $this->_url($url), $params);
  201.         }
  202.    
  203.         /**
  204.          * GET Request
  205.          *
  206.          * @param string $url
  207.          * @param array $params
  208.          * @return string
  209.          */
  210.         public function doGet($url, $params=array()) {
  211.             return $this->_exec(self::GET, $this->_url($url), $params);
  212.         }
  213.        
  214.         /**
  215.          * DELETE Request
  216.          *
  217.          * @param string $url
  218.          * @param array $params
  219.          * @return string
  220.          */
  221.         public function doDelete($url, $params=array()) {
  222.             return $this->_exec(self::DELETE, $this->_url($url), $params);
  223.         }
  224.    
  225.         private $_headers = array();
  226.        
  227.         /**
  228.          * setHeaders
  229.          *
  230.          * @param array $headers
  231.          * @return Http
  232.          */
  233.         public function setHeaders($headers) {
  234.             $this->_headers = $headers;
  235.             return $this;
  236.         }
  237.    
  238.         /**
  239.          * Builds absolute url
  240.          *
  241.          * @param unknown_type $url
  242.          * @return unknown
  243.          */
  244.         private function _url($url=null) {
  245.             return "{$this->_protocol}://{$this->_host}:{$this->_port}/{$url}";
  246.         }
  247.    
  248.         const HTTP_OK = 200;
  249.         const HTTP_CREATED = 201;
  250.         const HTTP_ACEPTED = 202;
  251.    
  252.         /**
  253.          * Performing the real request
  254.          *
  255.          * @param string $type
  256.          * @param string $url
  257.          * @param array $params
  258.          * @return string
  259.          */
  260.         private function _exec($type, $url, $params = array()) {
  261.             $headers = $this->_headers;
  262.             $s = curl_init();
  263.            
  264.             if (!is_null($this->_user)){
  265.                curl_setopt($s, CURLOPT_USERPWD, $this->_user.':'.$this->_pass);
  266.             }
  267.    
  268.             switch ($type) {
  269.                 case self::DELETE:
  270.                     curl_setopt($s, CURLOPT_URL, $url . '?' . http_build_query($params));
  271.                     curl_setopt($s, CURLOPT_CUSTOMREQUEST, self::DELETE);
  272.                     break;
  273.                 case self::PUT:
  274.                     curl_setopt($s, CURLOPT_URL, $url);
  275.                     curl_setopt($s, CURLOPT_CUSTOMREQUEST, self::PUT);
  276.                     curl_setopt($s, CURLOPT_POSTFIELDS, $params);
  277.                     break;
  278.                 case self::POST:
  279.                     curl_setopt($s, CURLOPT_URL, $url);
  280.                     curl_setopt($s, CURLOPT_POST, true);
  281.                     curl_setopt($s, CURLOPT_POSTFIELDS, $params);
  282.                     break;
  283.                 case self::GET:
  284.                     curl_setopt($s, CURLOPT_URL, $url . '?' . http_build_query($params));
  285.                     break;
  286.             }
  287.    
  288.             curl_setopt($s, CURLOPT_RETURNTRANSFER, true);
  289.             curl_setopt($s, CURLOPT_HTTPHEADER, $headers);
  290.             $_out = curl_exec($s);
  291.             $status = curl_getinfo($s, CURLINFO_HTTP_CODE);
  292.             curl_close($s);
  293.            
  294.             switch ($status) {
  295.                 case self::HTTP_OK:
  296.                 case self::HTTP_CREATED:
  297.                 case self::HTTP_ACEPTED:
  298.                     $out = $_out;
  299.                     break;
  300.                 default:
  301.                     if (!$this->_silentMode) {
  302.                         throw new Http_Exception("http error: {$status}", $status);
  303.                     }
  304.             }
  305.  
  306.             return $out;
  307.         }
  308.        
  309.         public function run() {
  310.             if ($this->_connMultiple) {
  311.                 return $this->_runMultiple();
  312.             } else {
  313.                 return $this->_run();
  314.             }
  315.         }
  316.        
  317.         private function _runMultiple() {
  318.             $out= null;
  319.             if (count($this->_append) > 0) {
  320.                 $arr = array();
  321.                 foreach ($this->_append as $_append) {
  322.                     $arr = array_merge($arr, $_append->_getRequests());
  323.                 }
  324.                
  325.                 $this->_requests = $arr;
  326.                 $out = $this->_run();
  327.             }
  328.             return $out;
  329.         }
  330.        
  331.         private function _run() {
  332.             $headers = $this->_headers;
  333.             $curly = $result = array();
  334.    
  335.             $mh = curl_multi_init();
  336.            
  337.             foreach ($this->_requests as $id => $reg) {
  338.                 $curly[$id] = curl_init();
  339.                
  340.                 $type = $reg[0];
  341.                 $url = $reg[1];
  342.                 $params = $reg[2];
  343.                
  344.                 if (!is_null($this->_user)) {
  345.                    curl_setopt($curly[$id], CURLOPT_USERPWD, $this->_user.':'.$this->_pass);
  346.                 }
  347.                
  348.                 switch ($type) {
  349.                     case self::DELETE:
  350.                         curl_setopt($curly[$id], CURLOPT_URL, $url . '?' . http_build_query($params));
  351.                         curl_setopt($curly[$id], CURLOPT_CUSTOMREQUEST, self::DELETE);
  352.                         break;
  353.                     case self::PUT:
  354.                         curl_setopt($curly[$id], CURLOPT_URL, $url);
  355.                         curl_setopt($curly[$id], CURLOPT_CUSTOMREQUEST, self::PUT);
  356.                         curl_setopt($curly[$id], CURLOPT_POSTFIELDS, $params);
  357.                         break;
  358.                     case self::POST:
  359.                         curl_setopt($curly[$id], CURLOPT_URL, $url);
  360.                         curl_setopt($curly[$id], CURLOPT_POST, true);
  361.                         curl_setopt($curly[$id], CURLOPT_POSTFIELDS, $params);
  362.                         break;
  363.                     case self::GET:
  364.                         curl_setopt($curly[$id], CURLOPT_URL, $url . '?' . http_build_query($params));
  365.                         break;
  366.                 }
  367.                
  368.                 curl_setopt($curly[$id], CURLOPT_TIMEOUT_MS, $this->_timeout);
  369.                 curl_setopt($curly[$id], CURLOPT_CONNECTTIMEOUT_MS, $this->_connectTimeout);
  370.                
  371.                 curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, true);
  372.                 curl_setopt($curly[$id], CURLOPT_HTTPHEADER, $headers);
  373.                
  374.                 curl_multi_add_handle($mh, $curly[$id]);
  375.             }
  376.        
  377.             $running = null;
  378.            
  379.             do {
  380.                 curl_multi_exec($mh, $running);
  381.                 usleep(25000);
  382.             } while ($running > 0);
  383.        
  384.             foreach ($curly as $id => $c) {
  385.                 $status = curl_getinfo($c, CURLINFO_HTTP_CODE);
  386.                 switch ($status) {
  387.                     case self::HTTP_OK:
  388.                     case self::HTTP_CREATED:
  389.                     case self::HTTP_ACEPTED:
  390.                         $result[$id] = curl_multi_getcontent($c);
  391.                         break;
  392.                     default:
  393.                         if (!$this->_silentMode) {
  394.                             $result[$id] = new Http_Multiple_Error($status, $type, $url, $params);
  395.                         }
  396.                 }
  397.                 curl_multi_remove_handle($mh, $c);
  398.             }
  399.    
  400.             curl_multi_close($mh);
  401.             return $result;
  402.         }
  403.     }
  404. ?>
  405.  
  406. [Window Title]
  407. Update Available
  408.  
  409. [Main Instruction]
  410. A new version of Sublime Text is available, download now?
  411.  
  412. [Download] [Cancel]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement