Advertisement
Guest User

Untitled

a guest
Apr 17th, 2012
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.58 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * OO cURL Class
  5. * Object oriented wrapper for the cURL library.
  6. * @author David Hopkins (semlabs.co.uk)
  7. * @version 0.3
  8. */
  9. class CURL
  10. {
  11.    
  12.     public $sessions                =   array();
  13.     public $retry                   =   0;
  14.    
  15.     /**
  16.     * Adds a cURL session to stack
  17.     * @param $url string, session's URL
  18.     * @param $opts array, optional array of cURL options and values
  19.     */
  20.     public function addSession( $url, $opts = false )
  21.     {
  22.         $this->sessions[] = curl_init( $url );
  23.         if( $opts != false )
  24.         {
  25.             $key = count( $this->sessions ) - 1;
  26.             $this->setOpts( $opts, $key );
  27.         }
  28.     }
  29.    
  30.  
  31.     /**
  32.     * Sets an array of options to a cURL session
  33.     * @param $options array, array of cURL options and values
  34.     * @param $key int, session key to set option for
  35.     */
  36.     public function setOpts( $options, $key = 0 )
  37.     {
  38.         curl_setopt_array( $this->sessions[$key], $options );
  39.     }
  40.    
  41.     /**
  42.     * Executes as cURL session
  43.     * @param $key int, optional argument if you only want to execute one session
  44.     */
  45.     public function exec( $key = false )
  46.     {
  47.         $no = count( $this->sessions );
  48.        
  49.         if( $no == 1 )
  50.        
  51.             $res = $this->execSingleReturnArray();
  52.         elseif( $no > 1 ) {
  53.             if( $key === false )
  54.                 $res = $this->execMulti(); 
  55.             else
  56.                 $res = $this->execSingle( $key );
  57.         }
  58.        
  59.         if( $res )
  60.             return $res;
  61.  
  62.     }
  63.    
  64.    
  65.        
  66.     /**
  67.     * Executes a stack of sessions
  68.     * @return array of content if CURLOPT_RETURNTRANSFER is set
  69.     */
  70.     public function execMulti()
  71.     {
  72.         $mh = curl_multi_init();
  73.        
  74.         #Add all sessions to multi handle
  75.         foreach ( $this->sessions as $i => $url )
  76.             curl_multi_add_handle( $mh, $this->sessions[$i] );
  77.        
  78.         do
  79.             $mrc = curl_multi_exec( $mh, $active );
  80.         while ( $mrc == CURLM_CALL_MULTI_PERFORM );
  81.        
  82.         while ( $active && $mrc == CURLM_OK )
  83.         {
  84.             if ( curl_multi_select( $mh ) != -1 )
  85.             {
  86.                 do
  87.                     $mrc = curl_multi_exec( $mh, $active );
  88.                 while ( $mrc == CURLM_CALL_MULTI_PERFORM );
  89.             }
  90.         }
  91.  
  92.         if ( $mrc != CURLM_OK )
  93.             echo "Curl multi read error $mrc\n";
  94.        
  95.         #Get content foreach session, retry if applied
  96.         foreach ( $this->sessions as $i => $url )
  97.         {
  98.            
  99.             $urlVero = $this->info( $i, CURLINFO_EFFECTIVE_URL );
  100.             $code = $this->info( $i, CURLINFO_HTTP_CODE );
  101.             if( $code[0] > 0 && $code[0] < 400 ) {
  102.                
  103.                 $res[] = curl_multi_getcontent( $this->sessions[$i] );
  104.                
  105.             }
  106.             else
  107.             {
  108.                 //RETRY DISABLED
  109.            
  110.                
  111.                     $theError = $this->error($i);
  112.                     $res[] = serialize(array('error_code' => '408', 'error'=> 'TIMEOUT[HTTP CODE '.$code[0].']', 'error_details' => $theError[0].' >>> FOR THIS REQUEST >>> '. $urlVero[0]));
  113.                
  114.                     //$res[] = false;
  115.             }
  116.  
  117.             curl_multi_remove_handle( $mh, $this->sessions[$i] );
  118.         }
  119.  
  120.         curl_multi_close( $mh );
  121.        
  122.         return $res;
  123.     }
  124.    
  125.     /**
  126.     * Closes cURL sessions
  127.     * @param $key int, optional session to close
  128.     */
  129.     public function close( $key = false )
  130.     {
  131.         if( $key === false )
  132.         {
  133.             foreach( $this->sessions as $session )
  134.                 curl_close( $session );
  135.         }
  136.         else
  137.             curl_close( $this->sessions[$key] );
  138.     }
  139.    
  140.     /**
  141.     * Remove all cURL sessions
  142.     */
  143.     public function clear()
  144.     {
  145.         foreach( $this->sessions as $session )
  146.             curl_close( $session );
  147.         unset( $this->sessions );
  148.     }
  149.    
  150.     /**
  151.     * Returns an array of session information
  152.     * @param $key int, optional session key to return info on
  153.     * @param $opt constant, optional option to return
  154.     */
  155.     public function info( $key = false, $opt = false )
  156.     {
  157.         if( $key === false )
  158.         {
  159.             foreach( $this->sessions as $key => $session )
  160.             {
  161.                 if( $opt )
  162.                     $info[] = curl_getinfo( $this->sessions[$key], $opt );
  163.                 else
  164.                     $info[] = curl_getinfo( $this->sessions[$key] );
  165.             }
  166.         }
  167.         else
  168.         {
  169.             if( $opt )
  170.                 $info[] = curl_getinfo( $this->sessions[$key], $opt );
  171.             else
  172.                 $info[] = curl_getinfo( $this->sessions[$key] );
  173.         }
  174.        
  175.         return $info;
  176.     }
  177.    
  178.     /**
  179.     * Returns an array of errors
  180.     * @param $key int, optional session key to retun error on
  181.     * @return array of error messages
  182.     */
  183.     public function error( $key = false )
  184.     {
  185.         if( $key === false )
  186.         {
  187.             foreach( $this->sessions as $session )
  188.                 $errors[] = curl_error( $session );
  189.         }
  190.         else
  191.             $errors[] = curl_error( $this->sessions[$key] );
  192.            
  193.         return $errors;
  194.     }
  195.    
  196.     /**
  197.     * Returns an array of session error numbers
  198.     * @param $key int, optional session key to retun error on
  199.     * @return array of error codes
  200.     */
  201.     public function errorNo( $key = false )
  202.     {
  203.         if( $key === false )
  204.         {
  205.             foreach( $this->sessions as $session )
  206.                 $errors[] = curl_errno( $session );
  207.         }
  208.         else
  209.             $errors[] = curl_errno( $this->sessions[$key] );
  210.            
  211.         return $errors;
  212.     }
  213.    
  214. }
  215.  
  216. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement