Advertisement
DraKiNs

[PHP] classe.curl.php reupload

Nov 9th, 2011
528
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.25 KB | None | 0 0
  1. <?php
  2. /**
  3.  * cURL OOP Wrapper.
  4.  * All cURL options (curl_setopt) are available
  5.  * through instance variables. All options can
  6.  * be found here: http://www.php.net/manual/en/function.curl-setopt.php
  7.  *
  8.  * <code>
  9.  * require_once("class.curl.php");
  10.  *
  11.  * $obj = new cURL("http://3floor.dk");
  12.  * $obj->ReturnTransfer = true;
  13.  * $obj->FollowLocation = true;
  14.  * echo $obj->exec();
  15.  * </code>
  16.  *
  17.  * This class also contains some static methods,
  18.  * designed for quick, easy HTTP request.
  19.  *
  20.  * <code>
  21.  * require_once("class.curl.php");
  22.  * // HTTP GET Request
  23.  * echo cURL::get("http://3floor.dk");
  24.  * // HTTP POST Request
  25.  * $data = array(
  26.  *  "var1" => "val1",
  27.  *  "var2" => "val2"
  28.  * );
  29.  * echo cURL::post("http://3floor.dk", $data);
  30.  * </code>
  31.  *
  32.  * @author Niklas Hansen <nikl@itu.dk>
  33.  * @version 0.2
  34.  */
  35. class cURL
  36. {
  37.     /**
  38.      * cURL
  39.      *
  40.      * @var object
  41.      * @access private
  42.      */
  43.     private $cURL;
  44.    
  45.     /**
  46.      * Array for cURL options
  47.      *
  48.      * @var array
  49.      * @access private
  50.      */
  51.     private $options = array();
  52.    
  53.     /**
  54.      * Constructor.
  55.      * Initializes cURL object.
  56.      *
  57.      * @access public
  58.      * @param string $url The URL for the request.
  59.      * @return void
  60.      */
  61.     function __construct ($url = NULL) {
  62.         return $this->cURL = curl_init($url);
  63.     }
  64.    
  65.     /**
  66.      * __set function
  67.      * Sets an option for the cURL request. If the option is already set, the
  68.      * existing option is removed, before setting the new value, in order
  69.      * to ensure the correct order of the set options.
  70.      *
  71.      * Usage:
  72.      * <code>
  73.      * require_once("class.curl.php");
  74.      *
  75.      * $obj = new cURL("http://3floor.dk");
  76.      * $obj->ReturnTransfer = true;
  77.      * $obj->FollowLocation = true;
  78.      * echo $obj->exec();
  79.      * </code>
  80.      *
  81.      *
  82.      * @access public
  83.      * @return void
  84.      */
  85.     function __set ($name, $value) {
  86.         if (array_key_exists(strtolower($name), $this->options)) {
  87.             unset($this->options[strtolower($name)]);
  88.         }
  89.        
  90.         $this->options[strtolower($name)] = $value;
  91.     }
  92.    
  93.     /**
  94.      * __get function
  95.      * Gets an option for the cURL request.
  96.      *
  97.      * @access public
  98.      * @return mixed Returns the value of the field requested. If field isn't
  99.      * set, null will be returned.
  100.      */
  101.     function __get ($name) {
  102.         if (array_key_exists(strtolower($name), $this->options)) {
  103.             return $this->options[strtolower($name)];
  104.         }
  105.        
  106.         return null;
  107.     }
  108.    
  109.     /**
  110.      * exec function.
  111.      * Executes the cURL HTTP Request.
  112.      *
  113.      * @access public
  114.      * @return mixed true/false on success/failure.
  115.      * When ReturnTranfer is used, the result of the
  116.      * request will be returned.
  117.      */
  118.     function exec () {
  119.         foreach ($this->options as $key => $val) {
  120.             if ($key !== "cURL" && $val !== null) {
  121.                 curl_setopt($this->cURL, constant(($key == "header_out" ? "CURLINFO_": "CURLOPT_").strtoupper($key)), $val);
  122.             }
  123.         }
  124.        
  125.         return curl_exec($this->cURL);
  126.     }
  127.    
  128.     /**
  129.      * errno function.
  130.      * Returns error-number from the last request.
  131.      *
  132.      * @access public
  133.      * @return int
  134.      */
  135.     function errno () {
  136.         return curl_errno($this->cURL);
  137.     }
  138.    
  139.     /**
  140.      * error function.
  141.      * Returns error message from the last request.
  142.      *
  143.      * @access public
  144.      * @return string
  145.      */
  146.     function error () {
  147.         return curl_error($this->cURL);
  148.     }
  149.    
  150.     /**
  151.      * getInfo function.
  152.      * Returns informations about the last request.
  153.      *
  154.      * @access public
  155.      * @param const $opt
  156.      * @return mixed
  157.      * @see http://php.net/manual/en/function.curl-getinfo.php
  158.      */
  159.     function getInfo ($opt = null) {
  160.         if ($opt === null) {
  161.             return curl_getinfo($this->cURL);
  162.         } else {
  163.             return curl_getinfo($this->cURL, $opt);
  164.         }
  165.     }
  166.    
  167.     /**
  168.      * cURL version.
  169.      * Returns information about the cURL version.
  170.      *
  171.      * @access public
  172.      * @static
  173.      * @return array
  174.      */
  175.     static function version () {
  176.         return curl_version();
  177.     }
  178.    
  179.     /**
  180.      * Destructor.
  181.      * Closes the cURL connection when the object
  182.      * is destructed.
  183.      *
  184.      * @access public
  185.      * @return void
  186.      */
  187.     function __destruct () {
  188.         curl_close($this->cURL);
  189.     }
  190.    
  191.    
  192.     /**************************/
  193.     /***** Static helpers *****/
  194.     /**************************/
  195.     /**
  196.      * HTTP GET Request.
  197.      * Makes a request to the specified URL,
  198.      * and returns the content. If URL
  199.      * does redirects, the script will follow
  200.      * the redirects.
  201.      *
  202.      * @access public
  203.      * @static
  204.      * @param string $url The URL for the request.
  205.      * @return mixed The content on success - false on failure.
  206.      */
  207.     static function get ($url) {
  208.         $class = __CLASS__;
  209.         $instance = new $class($url);
  210.         $instance->HTTPGet = true;
  211.         $instance->ReturnTransfer = true;
  212.         $instance->FollowLocation = true;
  213.         return $instance->exec();
  214.     }
  215.    
  216.     /**
  217.      * HTTP POST Request.
  218.      * Makes a POST request to the specified URL,
  219.      * and returns the content. The POST-data is
  220.      * given as an array.
  221.      *
  222.      * @access public
  223.      * @static
  224.      * @param string $url The URL for the request.
  225.      * @param array $data The data-array getting posted to $url.
  226.      * @return mixed The content on success - false on failure.
  227.      */
  228.     static function post ($url, $data) {
  229.         $class = __CLASS__;
  230.         $instance = new $class($url);
  231.         $instance->ReturnTransfer = true;
  232.         $instance->FollowLocation = true;
  233.         $instance->Post = 1;
  234.         $instance->PostFields = http_build_query($data);
  235.         return $instance->exec();
  236.     }
  237. }
  238.  
  239. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement