Source for file Curl.class.php

Documentation is available at Curl.class.php

  1. <?php
  2. /**
  3.  * Gumbo Library Framework
  4.  *
  5.  * LICENSE
  6.  * This library is being released under the terms of the New BSD License.  A
  7.  * copy of the license is packaged with the software (LICENSE.txt).  If no
  8.  * copy is found, a copy of the license template can be found at:
  9.  * http://www.opensource.org/licenses/bsd-license.php
  10.  * 
  11.  * @category Gumbo
  12.  * @package Curl
  13.  * @copyright Copyright (c) 2007, iBayou, Michael Luster
  14.  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  15.  * @author Michael Luster <mluster79@yahoo.com>
  16.  * @link http://sourceforge.net/projects/phpgumbo
  17.  * @version 0.0.1
  18.  */
  19.  
  20. /**
  21.  * Curl Class
  22.  *
  23.  * @category Gumbo
  24.  * @package Curl
  25.  * @copyright Copyright (c) 2007, iBayou, Michael Luster
  26.  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  27.  * @author Michael Luster <mluster79@yahoo.com>
  28.  * @link http://sourceforge.net/projects/phpgumbo
  29.  * @desc Curl Class
  30.  * @version 0.0.1
  31.  */
  32.  
  33. gumbo_load ("Interface_Curl");
  34.  
  35. class Gumbo_Curl implements Gumbo_Interface_Curl {
  36.     
  37.     /** @var resource $_curl CURL resource */
  38.     private $_curl;
  39.     /** @var string $_contents returned contents from the CURL call */
  40.     private $_contents;
  41.     /** @var array $_info */
  42.     private $_info = array ();
  43.     
  44.     
  45.     
  46.     /**
  47.      * Constructor: curl_init()
  48.      * @param string $url URL to call
  49.      */
  50.     public function __construct ($url=null{
  51.         $this->_curl = curl_init ($url);
  52.     }
  53.     
  54.     /**
  55.      * Destructor: curl_close()
  56.      */
  57.     public function __destruct ({
  58.         curl_close ($this->getCurl ());
  59.     }
  60.     
  61.     
  62.     
  63.     /** ACTION METHODS **/
  64.     /**
  65.      * Executes the CURL call, returning the response
  66.      * @return bool|string
  67.      */
  68.     public function execute ({
  69.         $this->setContents (curl_exec ($this->getCurl ()));
  70.         return $this->getContents ();
  71.     }
  72.     
  73.     
  74.     
  75.     /** MUTATOR METHODS **/
  76.     /**
  77.      * Sets the contents returned from a CURL call
  78.      * @param string $contents 
  79.      */
  80.     protected function setContents ($contents=null{
  81.         $this->_contents = $contents;
  82.     }
  83.     
  84.     /**
  85.      * Sets a CURL option
  86.      * @param int|array$option CURLOPT_* constant
  87.      * @param mixed $value primitive type value
  88.      * @throws Gumbo_Exception
  89.      * @todo verify constants against defined CURL constant types
  90.      */
  91.     public function setOption ($option$value=null{
  92.         try {
  93.             // verify precondition
  94.             if (!is_array ($option&& !is_int ($option)) {
  95.                 throw new Gumbo_Exception ("Invalid Argument 'option:int|arr' => {$option}:gettype ($option));
  96.             }
  97.             if (!is_null ($value&& (!is_numeric ($value&& !is_boolean ($value&& !is_string ($value))) {
  98.                 throw new Gumbo_Exception ("Invalid Argument 'value:bool|num|str|null' => {$value}:gettype ($value));
  99.             }
  100.             
  101.             // set the option
  102.             if (is_array ($option)) {
  103.                 curl_setopt_array ($this->getCurl ()$option);
  104.             else {
  105.                 curl_setopt ($this->getCurl ()$option$value);
  106.             }
  107.         catch (Gumbo_Exception $e{
  108.             $e->setFunction (__METHOD__);
  109.             gumbo_trigger ($e);
  110.         }
  111.     }
  112.     
  113.     
  114.     
  115.     /** ACCESSOR METHODS **/
  116.     /**
  117.      * Returns the CURL resource
  118.      * @return resource 
  119.      */
  120.     public function getCurl ({
  121.         return $this->_curl;
  122.     }
  123.     
  124.     /**
  125.      * Returns the contents returned from the CURL call
  126.      * @return string 
  127.      */
  128.     public function getContents ({
  129.         return $this->_contents;
  130.     }
  131.     
  132.     /**
  133.      * Returns the CURL version information
  134.      * @param int $age 
  135.      * @return array 
  136.      */
  137.     public function getVersion ($age=null{
  138.         return curl_version ($age);
  139.     }
  140.     
  141.     /**
  142.      * Returns information about CURL
  143.      * @param string $opt CURLINFO_* option
  144.      * @return array|string
  145.      */
  146.     public function getInfo ($opt=null{
  147.         return curl_getinfo ($this->getCurl ()$opt);
  148.     }
  149.     
  150. }
  151.  
  152. ?>