Advertisement
Guest User

Untitled

a guest
Sep 17th, 2017
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.82 KB | None | 0 0
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3.  * @author Philip Sturgeon
  4.  * @author Carey R. Dayrit <esourcehome.com>
  5.  * @created 9 Dec 2008
  6.  */
  7.  
  8. class Curl {
  9.    
  10.     private $CI;                // CodeIgniter instance
  11.  
  12.     private $responce;          // Contains the cURL responce for debug
  13.    
  14.     private $session;           // Contains the cURL handler for a session
  15.     private $url;               // URL of the session
  16.     private $options = array(); // Populates curl_setopt_array
  17.     private $headers = array(); // Populates extra HTTP headers
  18.    
  19.     public $error_code;         // Error code returned as an int
  20.     public $error_string;       // Error message returned as a string
  21.     public $info;               // Returned after request (elapsed time, etc)
  22.    
  23.     function __construct($url = '')
  24.     {
  25.         $this->CI =& get_instance();
  26.         log_message('debug', 'cURL Class Initialized');
  27.        
  28.         if (!function_exists('curl_init')) {
  29.             log_message('error', 'cURL Class - PHP was not built with cURL enabled. Rebuild PHP with --with-curl to use cURL.') ;
  30.         }
  31.        
  32.         if($url) $this->create($url);
  33.     }
  34.  
  35.     /* =================================================================================
  36.      * SIMPLE METHODS
  37.      * Using these methods you can make a quick and easy cURL call with one line.
  38.      * ================================================================================= */
  39.  
  40.     // Return a get request results
  41.     public function simple_get($url, $options = array())
  42.     {
  43.         // If a URL is provided, create new session
  44.         $this->create($url);
  45.  
  46.         // Add in the specific options provided
  47.         $this->options($options);
  48.  
  49.         return $this->execute();
  50.     }
  51.  
  52.     // Send a post request on its way with optional parameters (and get output)
  53.     // $url = '', $params = array(), $options = array()
  54.     public function simple_post($url, $params = array(), $options = array())
  55.     {
  56.         $this->create($url);
  57.        
  58.         $this->post($params, $options);
  59.        
  60.         return $this->execute();
  61.     }
  62.  
  63.     // Send a post request on its way with optional parameters (and get output)
  64.     // $url = '', $params = array(), $options = array()
  65.     public function simple_put($url, $params = array(), $options = array())
  66.     {
  67.         $this->create($url);
  68.        
  69.         $this->put($params, $options);
  70.        
  71.         return $this->execute();
  72.     }
  73.  
  74.     // Send a post request on its way with optional parameters (and get output)
  75.     // $url = '', $params = array(), $options = array()
  76.     public function simple_delete($url)
  77.     {
  78.         $this->create($url);
  79.        
  80.         $this->http_method('delete');
  81.                    
  82.         return $this->execute();
  83.     }
  84.    
  85.     public function simple_ftp_get($url, $file_path, $username = '', $password = '')
  86.     {
  87.         // If there is no ftp:// or any protocol entered, add ftp://
  88.         if(!preg_match('!^(ftp|sftp)://! i', $url)) {
  89.             $url = 'ftp://'.$url;
  90.         }
  91.        
  92.         // Use an FTP login
  93.         if($username != '')
  94.         {
  95.             $auth_string = $username;
  96.            
  97.             if($password != '')
  98.             {
  99.                 $auth_string .= ':'.$password;
  100.             }
  101.            
  102.             // Add the user auth string after the protocol
  103.             $url = str_replace('://', '://'.$auth_string.'@', $url);
  104.         }
  105.        
  106.         // Add the filepath
  107.         $url .= $file_path;
  108.  
  109.         $this->options(CURLOPT_BINARYTRANSFER, TRUE);
  110.         $this->options(CURLOPT_VERBOSE, TRUE);
  111.        
  112.         return $this->execute();
  113.     }
  114.    
  115.     /* =================================================================================
  116.      * ADVANCED METHODS
  117.      * Use these methods to build up more complex queries
  118.      * ================================================================================= */
  119.      
  120.     public function post($params = array(), $options = array()) {
  121.  
  122.         // If its an array (instead of a query string) then format it correctly
  123.         if(is_array($params)) {
  124.             $params = http_build_query($params,null,'&');
  125.         }
  126.         // Add in the specific options provided
  127.         $this->options($options);
  128.         $this->http_method('post');
  129.        
  130.         $this->option(CURLOPT_POST, TRUE);
  131.         $this->option(CURLOPT_POSTFIELDS, $params);
  132.     }
  133.    
  134.     public function put($params = array(), $options = array()) {
  135.        
  136.         // If its an array (instead of a query string) then format it correctly
  137.         if(is_array($params)) {
  138.             $params = http_build_query($params);
  139.         }
  140.        
  141.         // Add in the specific options provided
  142.         $this->options($options);
  143.        
  144.         $this->option(CURLOPT_PUT, TRUE);
  145.         $this->option(CURLOPT_POSTFIELDS, $params);
  146.     }
  147.    
  148.     public function set_cookies($params = array()) {
  149.        
  150.         if(is_array($params)) {
  151.             $params = http_build_query($params);
  152.         }
  153.        
  154.         $this->option(CURLOPT_COOKIE, $params);
  155.         return $this;
  156.     }
  157.  
  158.     public function get_headers(){
  159.         return $this->headers;
  160.     }
  161.    
  162.     public function http_header($header_string)
  163.     {
  164.         $this->headers[] = $header_string;
  165.     }
  166.    
  167.     public function http_method($method)
  168.     {
  169.         $this->options[CURLOPT_CUSTOMREQUEST] = strtoupper($method);
  170.         return $this;
  171.     }
  172.    
  173.     public function http_login($username = '', $password = '', $type = 'any')
  174.     {
  175.         $this->option(CURLOPT_HTTPAUTH, constant('CURLAUTH_'.strtoupper($type) ));
  176.         $this->option(CURLOPT_USERPWD, $username.':'.$password);
  177.         return $this;
  178.     }
  179.    
  180.     public function proxy($url = '', $port = 80) {
  181.        
  182.         $this->option(CURLOPT_HTTPPROXYTUNNEL. TRUE);
  183.         $this->option(CURLOPT_PROXY, $url.':'. 80);
  184.         return $this;
  185.     }
  186.    
  187.     public function proxy_login($username = '', $password = '') {
  188.         $this->option(CURLOPT_PROXYUSERPWD, $username.':'.$password);
  189.         return $this;
  190.     }
  191.    
  192.     public function options($options = array())
  193.     {
  194.         // Merge options in with the rest - done as array_merge() does not overwrite numeric keys
  195.         foreach($options as $option_code => $option_value)
  196.         {
  197.             $this->option($option_code, $option_value);
  198.         }
  199.         unset($option_code, $option_value);
  200.  
  201.         // Set all options provided
  202.         curl_setopt_array($this->session, $this->options);
  203.                
  204.         return $this;
  205.     }
  206.    
  207.     public function option($code, $value) {
  208.         $this->options[$code] = $value;
  209.         return $this;
  210.     }
  211.    
  212.     // Start a session from a URL
  213.     public function create($url) {
  214.        
  215.         // Reset the class
  216.         $this->set_defaults();
  217.  
  218.         // If no a protocol in URL, assume its a CI link
  219.         if(!preg_match('!^\w+://! i', $url)) {
  220.             $this->CI->load->helper('url');
  221.             $url = site_url($url);
  222.         }
  223.        
  224.         $this->url = $url;
  225.         $this->session = curl_init($this->url);
  226.        
  227.         return $this;
  228.     }
  229.    
  230.     // End a session and return the results
  231.     public function execute()
  232.     {
  233.         // Set two default options, and merge any extra ones in
  234.         if(!isset($this->options[CURLOPT_TIMEOUT]))           $this->options[CURLOPT_TIMEOUT] = 30;
  235.         if(!isset($this->options[CURLOPT_RETURNTRANSFER]))    $this->options[CURLOPT_RETURNTRANSFER] = TRUE;
  236.         if(!isset($this->options[CURLOPT_FOLLOWLOCATION]))    $this->options[CURLOPT_FOLLOWLOCATION] = FALSE;
  237.         if(!isset($this->options[CURLOPT_FAILONERROR]))       $this->options[CURLOPT_FAILONERROR] = TRUE;
  238.         if(!isset($this->options[CURLOPT_SSL_VERIFYHOST]))    $this->options[CURLOPT_SSL_VERIFYHOST]=FALSE;
  239.         if(!isset($this->options[CURLOPT_SSL_VERIFYPEER]))    $this->options[CURLOPT_SSL_VERIFYPEER]=FALSE;
  240.  
  241.         if(!empty($this->headers))
  242.         {
  243.             $this->option(CURLOPT_HTTPHEADER, $this->headers);
  244.         }
  245.  
  246.         $this->options();
  247.  
  248.         // Execute the request & and hide all output
  249.         $this->responce = curl_exec($this->session);
  250.  
  251.         // Request failed
  252.         if($this->responce === FALSE)
  253.         {
  254.             $this->error_code = curl_errno($this->session);
  255.             $this->error_string = curl_error($this->session);
  256.            
  257.             curl_close($this->session);
  258.             $this->session = NULL;
  259.             return FALSE;
  260.         }
  261.        
  262.         // Request successful
  263.         else
  264.         {
  265.             $this->info = curl_getinfo($this->session);
  266.            
  267.             curl_close($this->session);
  268.             $this->session = NULL;
  269.             return $this->responce;
  270.         }
  271.     }
  272.    
  273.    
  274.     public function debug()
  275.     {
  276.         echo "=============================================<br/>\n";
  277.         echo "<h2>CURL Test</h2>\n";
  278.         echo "=============================================<br/>\n";
  279.         echo "<h3>Responce</h3>\n";
  280.         echo "<code>".nl2br(htmlentities($this->responce))."</code><br/>\n\n";
  281.    
  282.         if($this->error_string)
  283.         {
  284.             echo "=============================================<br/>\n";
  285.             echo "<h3>Errors</h3>";
  286.             echo "<strong>Code:</strong> ".$this->error_code."<br/>\n";
  287.             echo "<strong>Message:</strong> ".$this->error_string."<br/>\n";
  288.         }
  289.    
  290.         echo "=============================================<br/>\n";
  291.         echo "<h3>Info</h3>";
  292.         echo "<pre>";
  293.         print_r($this->info);
  294.         echo "</pre>";
  295.     }
  296.    
  297.     private function set_defaults()
  298.     {
  299.         $this->responce = '';
  300.         $this->info = array();
  301.         $this->options = array();
  302.         $this->error_code = 0;
  303.         $this->error_string = '';
  304.     }
  305. }
  306. // END cURL Class
  307.  
  308. /* End of file cURL.php */
  309. /* Location: ./application/libraries/curl.php */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement