web/lib/Zend/Rest/Client.php
changeset 807 877f952ae2bd
parent 207 621fa6caec0c
child 1230 68c69c656a2c
equal deleted inserted replaced
805:5e7a0fedabdf 807:877f952ae2bd
    13  * to license@zend.com so we can send you a copy immediately.
    13  * to license@zend.com so we can send you a copy immediately.
    14  *
    14  *
    15  * @category   Zend
    15  * @category   Zend
    16  * @package    Zend_Rest
    16  * @package    Zend_Rest
    17  * @subpackage Client
    17  * @subpackage Client
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    18  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    20  * @version    $Id: Client.php 20096 2010-01-06 02:05:09Z bkarwin $
    20  * @version    $Id: Client.php 24593 2012-01-05 20:35:02Z matthew $
    21  */
    21  */
    22 
    22 
    23 
    23 
    24 /** Zend_Service_Abstract */
    24 /** Zend_Service_Abstract */
    25 require_once 'Zend/Service/Abstract.php';
    25 require_once 'Zend/Service/Abstract.php';
    32 
    32 
    33 /**
    33 /**
    34  * @category   Zend
    34  * @category   Zend
    35  * @package    Zend_Rest
    35  * @package    Zend_Rest
    36  * @subpackage Client
    36  * @subpackage Client
    37  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    37  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    38  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    38  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    39  */
    39  */
    40 class Zend_Rest_Client extends Zend_Service_Abstract
    40 class Zend_Rest_Client extends Zend_Service_Abstract
    41 {
    41 {
    42     /**
    42     /**
    48      /**
    48      /**
    49      * Zend_Uri of this web service
    49      * Zend_Uri of this web service
    50      * @var Zend_Uri_Http
    50      * @var Zend_Uri_Http
    51      */
    51      */
    52     protected $_uri = null;
    52     protected $_uri = null;
       
    53     
       
    54     /**
       
    55      * Flag indicating the Zend_Http_Client is fresh and needs no reset.
       
    56      * Must be set explicitly if you want to keep preset parameters.
       
    57      * @var bool true if you do not want a reset. Default false.
       
    58      */
       
    59     protected $_noReset = false;
    53 
    60 
    54     /**
    61     /**
    55      * Constructor
    62      * Constructor
    56      *
    63      *
    57      * @param string|Zend_Uri_Http $uri URI for the web service
    64      * @param string|Zend_Uri_Http $uri URI for the web service
    96      *
   103      *
    97      * @param  string $path            The path to append to the URI
   104      * @param  string $path            The path to append to the URI
    98      * @throws Zend_Rest_Client_Exception
   105      * @throws Zend_Rest_Client_Exception
    99      * @return void
   106      * @return void
   100      */
   107      */
   101     final private function _prepareRest($path)
   108     private function _prepareRest($path)
   102     {
   109     {
   103         // Get the URI object and configure it
   110         // Get the URI object and configure it
   104         if (!$this->_uri instanceof Zend_Uri_Http) {
   111         if (!$this->_uri instanceof Zend_Uri_Http) {
   105             require_once 'Zend/Rest/Client/Exception.php';
   112             require_once 'Zend/Rest/Client/Exception.php';
   106             throw new Zend_Rest_Client_Exception('URI object must be set before performing call');
   113             throw new Zend_Rest_Client_Exception('URI object must be set before performing call');
   116 
   123 
   117         /**
   124         /**
   118          * Get the HTTP client and configure it for the endpoint URI.  Do this each time
   125          * Get the HTTP client and configure it for the endpoint URI.  Do this each time
   119          * because the Zend_Http_Client instance is shared among all Zend_Service_Abstract subclasses.
   126          * because the Zend_Http_Client instance is shared among all Zend_Service_Abstract subclasses.
   120          */
   127          */
   121         self::getHttpClient()->resetParameters()->setUri($this->_uri);
   128         if ($this->_noReset) {
       
   129             // if $_noReset we do not want to reset on this request, 
       
   130             // but we do on any subsequent request
       
   131             $this->_noReset = false;
       
   132         } else {
       
   133             self::getHttpClient()->resetParameters();
       
   134         }
       
   135         
       
   136         self::getHttpClient()->setUri($this->_uri);
       
   137     }
       
   138     
       
   139     /**
       
   140      * Tells Zend_Rest_Client not to reset all parameters on it's 
       
   141      * Zend_Http_Client. If you want no reset, this must be called explicitly
       
   142      * before every request for which you do not want to reset the parameters.
       
   143      * Parameters will accumulate between requests, but as soon as you do not
       
   144      * call this function prior to any request, all preset parameters will be reset
       
   145      * as by default.
       
   146      * @param boolean $bool
       
   147      */
       
   148     public function setNoReset($bool = true)
       
   149     {
       
   150         $this->_noReset = $bool;
   122     }
   151     }
   123 
   152 
   124     /**
   153     /**
   125      * Performs an HTTP GET request to the $path.
   154      * Performs an HTTP GET request to the $path.
   126      *
   155      *
   127      * @param string $path
   156      * @param string $path
   128      * @param array  $query Array of GET parameters
   157      * @param array  $query Array of GET parameters
   129      * @throws Zend_Http_Client_Exception
   158      * @throws Zend_Http_Client_Exception
   130      * @return Zend_Http_Response
   159      * @return Zend_Http_Response
   131      */
   160      */
   132     final public function restGet($path, array $query = null)
   161     public function restGet($path, array $query = null)
   133     {
   162     {
   134         $this->_prepareRest($path);
   163         $this->_prepareRest($path);
   135         $client = self::getHttpClient();
   164         $client = self::getHttpClient();
   136         $client->setParameterGet($query);
   165         $client->setParameterGet($query);
   137         return $client->request('GET');
   166         return $client->request('GET');
   165      * @param string $path
   194      * @param string $path
   166      * @param mixed $data Raw data to send
   195      * @param mixed $data Raw data to send
   167      * @throws Zend_Http_Client_Exception
   196      * @throws Zend_Http_Client_Exception
   168      * @return Zend_Http_Response
   197      * @return Zend_Http_Response
   169      */
   198      */
   170     final public function restPost($path, $data = null)
   199     public function restPost($path, $data = null)
   171     {
   200     {
   172         $this->_prepareRest($path);
   201         $this->_prepareRest($path);
   173         return $this->_performPost('POST', $data);
   202         return $this->_performPost('POST', $data);
   174     }
   203     }
   175 
   204 
   179      * @param string $path
   208      * @param string $path
   180      * @param mixed $data Raw data to send in request
   209      * @param mixed $data Raw data to send in request
   181      * @throws Zend_Http_Client_Exception
   210      * @throws Zend_Http_Client_Exception
   182      * @return Zend_Http_Response
   211      * @return Zend_Http_Response
   183      */
   212      */
   184     final public function restPut($path, $data = null)
   213     public function restPut($path, $data = null)
   185     {
   214     {
   186         $this->_prepareRest($path);
   215         $this->_prepareRest($path);
   187         return $this->_performPost('PUT', $data);
   216         return $this->_performPost('PUT', $data);
   188     }
   217     }
   189 
   218 
   192      *
   221      *
   193      * @param string $path
   222      * @param string $path
   194      * @throws Zend_Http_Client_Exception
   223      * @throws Zend_Http_Client_Exception
   195      * @return Zend_Http_Response
   224      * @return Zend_Http_Response
   196      */
   225      */
   197     final public function restDelete($path)
   226     public function restDelete($path, $data = null)
   198     {
   227     {
   199         $this->_prepareRest($path);
   228         $this->_prepareRest($path);
   200         return self::getHttpClient()->request('DELETE');
   229         return $this->_performPost('DELETE', $data);
   201     }
   230     }
   202 
   231 
   203     /**
   232     /**
   204      * Method call overload
   233      * Method call overload
   205      *
   234      *