예제 #1
0
 /**
  * Performs an HTTP PUT request to $path.
  *
  * @param string $path
  * @param array $data
  * @return Zend_Http_Response
  */
 public final function restPut($path, $data)
 {
     $this->_prepareRest($path);
     $this->_uri->setQuery($data);
     self::getHttpClient()->setRawData($data);
     return self::getHttpClient()->request('PUT');
 }
예제 #2
0
 /**
  * Validate OAuth request
  * @param Zend_Uri_Http $url Request URL, will use current if null
  * @param array $params Additional parameters
  * @return bool
  * @throws Zend_Oauth_Exception
  */
 public function checkOAuthRequest(Zend_Uri_Http $url = null, $params = array())
 {
     if (empty($url)) {
         $this->url = $this->getRequestUrl();
     } else {
         $this->url = clone $url;
     }
     // We'll ignore query for the pruposes of URL matching
     $this->url->setQuery('');
     if (isset($_SERVER['REQUEST_METHOD'])) {
         $method = $_SERVER['REQUEST_METHOD'];
     } elseif (isset($_SERVER['HTTP_METHOD'])) {
         $method = $_SERVER['HTTP_METHOD'];
     } else {
         $method = 'GET';
     }
     $params = $this->assembleParams($method, $params);
     $this->checkSignatureMethod($params['oauth_signature_method']);
     $this->checkRequiredParams($params);
     $this->timestamp = $params['oauth_timestamp'];
     $this->nonce = $params['oauth_nonce'];
     $this->consumer_key = $params['oauth_consumer_key'];
     if (!is_callable($this->nonceHandler)) {
         throw new Zend_Oauth_Exception("Nonce handler not callable", self::BAD_NONCE);
     }
     $res = call_user_func($this->nonceHandler, $this);
     if ($res != self::OK) {
         throw new Zend_Oauth_Exception("Invalid request", $res);
     }
     if (!is_callable($this->consumerHandler)) {
         throw new Zend_Oauth_Exception("Consumer handler not callable", self::CONSUMER_KEY_UNKNOWN);
     }
     $res = call_user_func($this->consumerHandler, $this);
     // this will set $this->consumer_secret if OK
     if ($res != self::OK) {
         throw new Zend_Oauth_Exception("Consumer key invalid", $res);
     }
     if ($this->needsToken()) {
         $this->token = $params['oauth_token'];
         $this->verifier = $params['oauth_verifier'];
         if (!is_callable($this->tokenHandler)) {
             throw new Zend_Oauth_Exception("Token handler not callable", self::TOKEN_REJECTED);
         }
         $res = call_user_func($this->tokenHandler, $this);
         // this will set $this->token_secret if OK
         if ($res != self::OK) {
             throw new Zend_Oauth_Exception("Token invalid", $res);
         }
     }
     $util = new Zend_Oauth_Http_Utility();
     $req_sign = $params['oauth_signature'];
     unset($params['oauth_signature']);
     $our_sign = $util->sign($params, $params['oauth_signature_method'], $this->consumer_secret, $this->token_secret, $method, $this->url->getUri());
     if ($req_sign != $our_sign) {
         // TODO: think how to extract signature base string
         $this->problem = $our_sign;
         throw new Zend_Oauth_Exception("Invalid signature", self::INVALID_SIGNATURE);
     }
     return true;
 }
예제 #3
0
 /**
  * Call a remote REST web service URI and return the Zend_Http_Response object
  *
  * @param  string $path            The path to append to the URI
  * @throws Zend_Service_Exception
  * @return void
  */
 private final function _prepareRest($path, $query = null)
 {
     // Get the URI object and configure it
     if (!$this->_uri instanceof Zend_Uri_Http) {
         throw new Zend_Service_Exception('URI object must be set before performing call');
     }
     $uri = $this->_uri->getUri();
     if ($path[0] != '/' && $uri[strlen($uri) - 1] != '/') {
         $path = '/' . $path;
     }
     $this->_uri->setPath($path);
     if (!is_null($query)) {
         $this->_uri->setQuery($query);
     }
     /**
      * Get the HTTP client and configure it for the endpoint URI.  Do this each time
      * because the Zend_Http_Client instance is shared among all Zend_Service_Abstract subclasses.
      */
     self::getHttpClient()->setUri($this->_uri);
 }
예제 #4
0
 /**
  * Call a remote REST web service URI and return the Zend_Http_Response object
  *
  * @param  string $path            The path to append to the URI
  * @throws Zend_Rest_Client_Exception
  * @return void
  */
 private final function _prepareRest($path)
 {
     // Get the URI object and configure it
     if (!$this->_uri instanceof Zend_Uri_Http) {
         require_once 'Zend/Rest/Client/Exception.php';
         throw new Zend_Rest_Client_Exception('URI object must be set before performing call');
     }
     list($path, $query) = explode("?", $path);
     $uri = $this->_uri->getUri();
     if ($path[0] != '/') {
         $path = $this->_basepath . $path;
     }
     $this->_uri->setPath($path);
     $this->_uri->setQuery($query);
     /**
      * Get the HTTP client and configure it for the endpoint URI.  Do this each time
      * because the Zend_Http_Client instance is shared among all Zend_Service_Abstract subclasses.
      */
     self::getHttpClient()->resetParameters()->setUri($this->_uri);
 }
예제 #5
0
 /**
  * Performs a Twitter search query.
  *
  * @throws Zend_Http_Client_Exception
  */
 public function search($query, array $params = array())
 {
     $this->_uri->setPath('/search.' . $this->_responseType);
     $this->_uri->setQuery(null);
     $_query = array();
     $_query['q'] = $query;
     foreach ($params as $key => $param) {
         switch ($key) {
             case 'geocode':
             case 'lang':
             case 'since_id':
                 $_query[$key] = $param;
                 break;
             case 'rpp':
                 $_query[$key] = intval($param) > 100 ? 100 : intval($param);
                 break;
             case 'page':
                 $_query[$key] = intval($param);
                 break;
             case 'show_user':
                 $_query[$key] = 'true';
         }
     }
     $this->_uri->setQuery($_query);
     $this->setUri($this->_uri);
     $response = $this->request();
     switch ($this->_responseType) {
         case 'json':
             return Zend_Json::decode($response->getBody());
             break;
         case 'atom':
             return Zend_Feed::importString($response->getBody());
             break;
     }
     return;
 }
예제 #6
0
 /**
  * Send the HTTP request and return an HTTP response object
  *
  * @param string $method
  * @return Zend_Http_Response
  * @throws Zend_Http_Client_Exception
  */
 public function request($method = null)
 {
     if (!$this->uri instanceof Zend_Uri_Http) {
         /** @see Zend_Http_Client_Exception */
         require_once 'Zend/Http/Client/Exception.php';
         throw new Zend_Http_Client_Exception('No valid URI has been passed to the client');
     }
     if ($method) {
         $this->setMethod($method);
     }
     $this->redirectCounter = 0;
     $response = null;
     // Make sure the adapter is loaded
     if ($this->adapter == null) {
         $this->setAdapter($this->config['adapter']);
     }
     // Send the first request. If redirected, continue.
     do {
         // Clone the URI and add the additional GET parameters to it
         $uri = clone $this->uri;
         if (!empty($this->paramsGet)) {
             $query = $uri->getQuery();
             if (!empty($query)) {
                 $query .= '&';
             }
             $query .= http_build_query($this->paramsGet, null, '&');
             if ($this->config['rfc3986_strict']) {
                 $query = str_replace('+', '%20', $query);
             }
             // @see ZF-11671 to unmask for some services to foo=val1&foo=val2
             if ($this->getUnmaskStatus()) {
                 if ($this->_queryBracketsEscaped) {
                     $query = preg_replace('/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', '=', $query);
                 } else {
                     $query = preg_replace('/\\[(?:[0-9]|[1-9][0-9]+)\\]=/', '=', $query);
                 }
             }
             $uri->setQuery($query);
         }
         $body = $this->_prepareBody();
         $headers = $this->_prepareHeaders();
         // check that adapter supports streaming before using it
         if (is_resource($body) && !$this->adapter instanceof Zend_Http_Client_Adapter_Stream) {
             /** @see Zend_Http_Client_Exception */
             require_once 'Zend/Http/Client/Exception.php';
             throw new Zend_Http_Client_Exception('Adapter does not support streaming');
         }
         // Open the connection, send the request and read the response
         $this->adapter->connect($uri->getHost(), $uri->getPort(), $uri->getScheme() == 'https' ? true : false);
         if ($this->config['output_stream']) {
             if ($this->adapter instanceof Zend_Http_Client_Adapter_Stream) {
                 $stream = $this->_openTempStream();
                 $this->adapter->setOutputStream($stream);
             } else {
                 /** @see Zend_Http_Client_Exception */
                 require_once 'Zend/Http/Client/Exception.php';
                 throw new Zend_Http_Client_Exception('Adapter does not support streaming');
             }
         }
         $this->last_request = $this->adapter->write($this->method, $uri, $this->config['httpversion'], $headers, $body);
         $response = $this->adapter->read();
         if (!$response) {
             /** @see Zend_Http_Client_Exception */
             require_once 'Zend/Http/Client/Exception.php';
             throw new Zend_Http_Client_Exception('Unable to read response, or response is empty');
         }
         if ($this->config['output_stream']) {
             $streamMetaData = stream_get_meta_data($stream);
             if ($streamMetaData['seekable']) {
                 rewind($stream);
             }
             // cleanup the adapter
             $this->adapter->setOutputStream(null);
             $response = Zend_Http_Response_Stream::fromStream($response, $stream);
             $response->setStreamName($this->_stream_name);
             if (!is_string($this->config['output_stream'])) {
                 // we used temp name, will need to clean up
                 $response->setCleanup(true);
             }
         } else {
             $response = Zend_Http_Response::fromString($response);
         }
         if ($this->config['storeresponse']) {
             $this->last_response = $response;
         }
         // Load cookies into cookie jar
         if (isset($this->cookiejar)) {
             $this->cookiejar->addCookiesFromResponse($response, $uri, $this->config['encodecookies']);
         }
         // If we got redirected, look for the Location header
         if ($response->isRedirect() && ($location = $response->getHeader('location'))) {
             // Avoid problems with buggy servers that add whitespace at the
             // end of some headers (See ZF-11283)
             $location = trim($location);
             // Check whether we send the exact same request again, or drop the parameters
             // and send a GET request
             if ($response->getStatus() == 303 || !$this->config['strictredirects'] && ($response->getStatus() == 302 || $response->getStatus() == 301)) {
                 $this->resetParameters();
                 $this->setMethod(self::GET);
             }
             // If we got a well formed absolute URI
             if (($scheme = substr($location, 0, 6)) && ($scheme == 'http:/' || $scheme == 'https:')) {
                 $this->setHeaders('host', null);
                 $this->setUri($location);
             } else {
                 // Split into path and query and set the query
                 if (strpos($location, '?') !== false) {
                     list($location, $query) = explode('?', $location, 2);
                 } else {
                     $query = '';
                 }
                 $this->uri->setQuery($query);
                 // Else, if we got just an absolute path, set it
                 if (strpos($location, '/') === 0) {
                     $this->uri->setPath($location);
                     // Else, assume we have a relative path
                 } else {
                     // Get the current path directory, removing any trailing slashes
                     $path = $this->uri->getPath();
                     $path = rtrim(substr($path, 0, strrpos($path, '/')), "/");
                     $this->uri->setPath($path . '/' . $location);
                 }
             }
             ++$this->redirectCounter;
         } else {
             // If we didn't get any location, stop redirecting
             break;
         }
     } while ($this->redirectCounter < $this->config['maxredirects']);
     return $response;
 }
예제 #7
0
 /**
  * Send the HTTP request and return an HTTP response object
  *
  * @param string $method
  * @return Zend_Http_Response
  * @throws Zend_Http_Client_Exception
  */
 public function request($method = null)
 {
     if (!$this->uri instanceof Zend_Uri_Http) {
         /** @see Zend_Http_Client_Exception */
         require_once 'Zend/Http/Client/Exception.php';
         throw new Zend_Http_Client_Exception('No valid URI has been passed to the client');
     }
     if ($method) {
         $this->setMethod($method);
     }
     $this->redirectCounter = 0;
     $response = null;
     // Make sure the adapter is loaded
     if ($this->adapter == null) {
         $this->setAdapter($this->config['adapter']);
     }
     // Send the first request. If redirected, continue.
     do {
         // Clone the URI and add the additional GET parameters to it
         $uri = clone $this->uri;
         if (!empty($this->paramsGet)) {
             $query = $uri->getQuery();
             if (!empty($query)) {
                 $query .= '&';
             }
             $query .= http_build_query($this->paramsGet, null, '&');
             $uri->setQuery($query);
         }
         $body = $this->_prepareBody();
         $headers = $this->_prepareHeaders();
         // Open the connection, send the request and read the response
         $this->adapter->connect($uri->getHost(), $uri->getPort(), $uri->getScheme() == 'https' ? true : false);
         $this->last_request = $this->adapter->write($this->method, $uri, $this->config['httpversion'], $headers, $body);
         $response = $this->adapter->read();
         if (!$response) {
             /** @see Zend_Http_Client_Exception */
             require_once 'Zend/Http/Client/Exception.php';
             throw new Zend_Http_Client_Exception('Unable to read response, or response is empty');
         }
         $response = Zend_Http_Response::fromString($response);
         if ($this->config['storeresponse']) {
             $this->last_response = $response;
         }
         // Load cookies into cookie jar
         if (isset($this->cookiejar)) {
             $this->cookiejar->addCookiesFromResponse($response, $uri);
         }
         // If we got redirected, look for the Location header
         if ($response->isRedirect() && ($location = $response->getHeader('location'))) {
             // Check whether we send the exact same request again, or drop the parameters
             // and send a GET request
             if ($response->getStatus() == 303 || !$this->config['strictredirects'] && ($response->getStatus() == 302 || $response->getStatus() == 301)) {
                 $this->resetParameters();
                 $this->setMethod(self::GET);
             }
             // If we got a well formed absolute URI
             if (Zend_Uri_Http::check($location)) {
                 $this->setHeaders('host', null);
                 $this->setUri($location);
             } else {
                 // Split into path and query and set the query
                 if (strpos($location, '?') !== false) {
                     list($location, $query) = explode('?', $location, 2);
                 } else {
                     $query = '';
                 }
                 $this->uri->setQuery($query);
                 // Else, if we got just an absolute path, set it
                 if (strpos($location, '/') === 0) {
                     $this->uri->setPath($location);
                     // Else, assume we have a relative path
                 } else {
                     // Get the current path directory, removing any trailing slashes
                     $path = $this->uri->getPath();
                     $path = rtrim(substr($path, 0, strrpos($path, '/')), "/");
                     $this->uri->setPath($path . '/' . $location);
                 }
             }
             ++$this->redirectCounter;
         } else {
             // If we didn't get any location, stop redirecting
             break;
         }
     } while ($this->redirectCounter < $this->config['maxredirects']);
     return $response;
 }