[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/net/src/ -> lmbCurlRequest.class.php (source)

   1  <?php
   2  /*
   3   * Limb PHP Framework
   4   *
   5   * @link http://limb-project.com 
   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
   8   */
   9  
  10  /**

  11   * class lmbCurlRequest.

  12   *

  13   * @package net

  14   * @version $Id$

  15   */
  16  class lmbCurlRequest
  17  {
  18    protected $url = '';
  19    protected $handle;
  20    protected $opts = array();
  21  
  22    function __construct($url)
  23    {
  24      $this->url = $url;
  25      $this->_initDefaultOptions();
  26    }
  27  
  28    function open($post_data = '')
  29    {
  30      $this->_ensureCurl();
  31      if($post_data)
  32        $this->_setPostData($post_data);
  33  
  34      $this->_setupCurlOptions();
  35      return $this->_exec();
  36    }
  37  
  38    function setOpt($opt, $value)
  39    {
  40      $this->opts[$opt] = $value;
  41    }
  42  
  43    protected function _initDefaultOptions()
  44    {
  45      $this->opts = array(CURLOPT_HEADER => 0,
  46                          CURLOPT_RETURNTRANSFER => 1,
  47                          CURLOPT_URL => $this->url);
  48    }
  49  
  50    protected function _ensureCurl()
  51    {
  52      if(!is_resource($this->handle))
  53       $this->handle = curl_init();
  54    }
  55  
  56    protected function _setupCurlOptions()
  57    {
  58      foreach($this->opts as $opt => $value)
  59        curl_setopt($this->handle, $opt, $value);
  60    }
  61  
  62    protected function _exec()
  63    {
  64      $res = curl_exec($this->handle);
  65      $this->_resetCurl();
  66      return $res;
  67    }
  68  
  69    protected function _resetCurl()
  70    {
  71      if(is_resource($this->handle))
  72        curl_close($this->handle);
  73      $this->opts = array();
  74    }
  75  
  76    protected function _setPostData($post_data)
  77    {
  78      if(!$post_data)
  79        return;
  80  
  81      $this->setOpt(CURLOPT_POST, 1);
  82  
  83      $var_string = '';
  84      foreach ($post_data as $k => $v)
  85        if(is_array($v))
  86        {
  87          foreach($v as $value)
  88          $var_string .= "{$k}[]={$value}&";
  89        }
  90        else
  91          $var_string .= "{$k}={$v}&";
  92  
  93      $this->setOpt(CURLOPT_POSTFIELDS, $var_string);
  94    }
  95  }
  96  
  97  ?>


Generated: Tue Oct 14 04:47:40 2008 Cross-referenced by PHPXref 0.7