Esempio n. 1
0
 /**
  * Sends a request to the REST API service and does initial processing
  * on the response.
  *
  * @param  string       $op    Name of the operation for the request
  * @param  string|array $query Query data for the request (optional)
  * @throws Zend_Service_Exception
  * @return DOMDocument Parsed XML response
  */
 protected function _makeRequest($op, $query = null)
 {
     if ($query != null) {
         $query = array_diff($query, array_filter($query, 'is_null'));
     }
     $response = $this->_rest->restGet('/simpy/api/rest/' . $op . '.do', $query);
     if ($response->isSuccessful()) {
         $doc = new DOMDocument();
         $doc->loadXML($response->getBody());
         $xpath = new DOMXPath($doc);
         $list = $xpath->query('/status/code');
         if ($list->length > 0) {
             $code = $list->item(0)->nodeValue;
             if ($code != 0) {
                 $list = $xpath->query('/status/message');
                 $message = $list->item(0)->nodeValue;
                 /**
                  * @see Zend_Service_Exception
                  */
                 require_once 'Zend/Service/Exception.php';
                 throw new Zend_Service_Exception($message, $code);
             }
         }
         return $doc;
     }
     /**
      * @see Zend_Service_Exception
      */
     require_once 'Zend/Service/Exception.php';
     throw new Zend_Service_Exception('HTTP ' . $response->getStatus());
 }
Esempio n. 2
0
 /**
  * Handles all GET requests to a web service
  *
  * @param   string $path  Path
  * @param   array  $parms Array of GET parameters
  * @param   string $type  Type of a request ("xml"|"json")
  * @return  mixed  decoded response from web service
  * @throws  Zend_Service_Delicious_Exception
  */
 public function makeRequest($path, array $parms = array(), $type = 'xml')
 {
     // if previous request was made less then 1 sec ago
     // wait until we can make a new request
     $timeDiff = microtime(true) - self::$_lastRequestTime;
     if ($timeDiff < 1) {
         //TODO:trouver
         //usleep((1 - $timeDiff) * 1000000);
     }
     $this->_rest->getHttpClient()->setAuth($this->_authUname, $this->_authPass);
     switch ($type) {
         case 'xml':
             $this->_rest->setUri(self::API_URI);
             break;
         case 'json':
             $parms['raw'] = true;
             $this->_rest->setUri(self::JSON_URI);
             break;
         default:
             /**
              * @see Zend_Service_Delicious_Exception
              */
             require_once PHP_LIBRARY_PATH . 'Zend/Service/Delicious/Exception.php';
             throw new Zend_Service_Delicious_Exception('Unknown request type');
     }
     self::$_lastRequestTime = microtime(true);
     $response = $this->_rest->restGet($path, $parms);
     if (!$response->isSuccessful()) {
         /**
          * @see Zend_Service_Delicious_Exception
          */
         require_once PHP_LIBRARY_PATH . 'Zend/Service/Delicious/Exception.php';
         throw new Zend_Service_Delicious_Exception("Http client reported an error: '{$response->getMessage()}'");
     }
     $responseBody = $response->getBody();
     switch ($type) {
         case 'xml':
             $dom = new DOMDocument();
             if (!@$dom->loadXML($responseBody)) {
                 /**
                  * @see Zend_Service_Delicious_Exception
                  */
                 require_once PHP_LIBRARY_PATH . 'Zend/Service/Delicious/Exception.php';
                 throw new Zend_Service_Delicious_Exception('XML Error');
             }
             return $dom;
         case 'json':
             return Zend_Json_Decoder::decode($responseBody);
     }
 }
Esempio n. 3
0
 /**
  * Utility function to find Flickr photo details by ID.
  * @param string $id the NSID
  * @return Zend_Service_Flickr_Image the details for the specified image
  */
 public function getImageDetails($id)
 {
     static $method = 'flickr.photos.getSizes';
     $options = array('api_key' => $this->apiKey, 'method' => $method, 'photo_id' => $id);
     if (!empty($id)) {
         $response = $this->_rest->restGet('/services/rest/', $options);
         $dom = new DOMDocument();
         $dom->loadXML($response->getBody());
         $xpath = new DOMXPath($dom);
         self::_checkErrors($dom);
         $return = array();
         foreach ($xpath->query('//size') as $size) {
             $label = (string) $size->getAttribute('label');
             $retval[$label] = new Zend_Service_Flickr_Image($size);
         }
     } else {
         throw new Zend_Service_Exception('You must supply a photo ID');
     }
     return $retval;
 }
 /**
  *
  * Private method that queries REST service and returns SimpleXML response set
  * @param string $service name of Audioscrobbler service file we're accessing
  * @param string $params parameters that we send to the service if needded
  * @return SimpleXML result set
  */
 private function getInfo($service, $params = NULL)
 {
     $service = (string) $service;
     $params = (string) $params;
     try {
         $request = $this->_rest->restGet($service, $params);
         if ($request->isSuccessful()) {
             $response = simplexml_load_string($request->getBody());
             return $response;
         } else {
             if ($request->getBody() == 'No such path') {
                 throw new Zend_Service_Exception('Could not find: ' . $dir);
             } else {
                 if ($request->getBody() == 'No user exists with this name.') {
                     throw new Zend_Service_Exception('No user exists with this name.');
                 } else {
                     throw new Zend_Service_Exception('The REST service ' . $service . ' returned the following status code: ' . $request->getStatus());
                 }
             }
         }
     } catch (Zend_Service_Exception $e) {
         throw $e;
     }
 }
Esempio n. 5
0
 /**
  * Perform a web content search on search.yahoo.com.  A basic query
  * consists simply of a text query.  Additional options that can be
  * specified consist of:
  * 'results'    => int  How many results to return, max is 50
  * 'start'      => int  The start offset for search results
  * 'language'   => lang  The target document language to match
  * 'type'       => (all|any|phrase)  How the query should be parsed
  * 'site'       => string  A site to which your search should be restricted
  * 'format'     => (any|html|msword|pdf|ppt|rss|txt|xls)
  * 'adult_ok'   => bool  permit 'adult' content in the search results
  * 'similar_ok' => bool  permit similar results in the result set
  * 'country'    => string  The country code for the content searched
  * 'license'    => (any|cc_any|cc_commercial|cc_modifiable)  The license of content being searched
  *
  * @param string $query  the query being run
  * @param array $options  any optional parameters
  * @return Zend_Service_Yahoo_WebResultSet  The return set
  */
 public function webSearch($query, $options = NULL)
 {
     static $default_options = array('type' => 'all', 'start' => 1, 'language' => 'en', 'license' => 'any', 'results' => 10, 'format' => 'any');
     $options = $this->_prepareOptions($query, $options, $default_options);
     $this->_validateWebSearch($options);
     $this->_uri->setHost('api.search.yahoo.com');
     $response = $this->_rest->restGet('/WebSearchService/V1/webSearch', $options);
     if ($response->isError()) {
         throw new Zend_Service_Exception('An error occurred sending request. Status code: ' . $response->getStatus());
     }
     $dom = new DOMDocument();
     $dom->loadXML($response->getBody());
     self::_checkErrors($dom);
     return new Zend_Service_Yahoo_WebResultSet($dom);
 }
Esempio n. 6
0
 /**
  * Look up for a Single Item
  *
  * @param string $asin Amazon ASIN ID
  * @param array $options Query Options
  * @see http://www.amazon.com/gp/aws/sdk/main.html/102-9041115-9057709?s=AWSEcommerceService&v=2005-10-05&p=ApiReference/ItemLookupOperation
  * @throws Zend_Service_Exception
  * @return Zend_Service_Amazon_Item|null
  */
 public function itemLookup($asin, $options = null)
 {
     if (!$options) {
         $options = array();
     }
     $defaultOptions = array('IdType' => 'ASIN', 'ResponseGroup' => 'Small');
     $options['ItemId'] = $asin;
     $options = $this->_prepareOptions('ItemLookup', $options, $defaultOptions);
     $this->_validateItemLookup($options);
     $response = $this->_rest->restGet('/onca/xml', $options);
     if ($response->isError()) {
         throw new Zend_Service_Exception('An error occurred sending request. Status code: ' . $response->getStatus());
     }
     $dom = new DOMDocument();
     $dom->loadXML($response->getBody());
     self::_checkErrors($dom);
     $xpath = new DOMXPath($dom);
     $xpath->registerNamespace('az', 'http://webservices.amazon.com/AWSECommerceService/2005-10-05');
     $items = $xpath->query('//az:Items/az:Item');
     if ($items->length == 1) {
         return new Zend_Service_Amazon_Item($items->item(0));
     }
     return null;
 }
Esempio n. 7
0
 /**
  * Executes an API request and returns the response.
  *
  * @param Zend_Service_RememberTheMilk_Request $request
  * @throws Zend_Service_Exception
  * @return array Associative array containing the response data
  */
 protected function _request($request)
 {
     $params = $request->getParameters();
     $params['api_key'] = $this->_apiKey;
     $params['format'] = 'json';
     $params['method'] = $request->getMethod();
     if ($request->requiresTimeline()) {
         if ($this->_timeline == null) {
             $timeline = new Zend_Service_RememberTheMilk_Request();
             $timeline->setMethod('rtm.timelines.create');
             $timeline->useTimeline(false);
             $response = $this->_request($timeline);
             $this->_timeline = $response->timeline;
         }
         $params['timeline'] = $this->_timeline;
     }
     if ($request->requiresAuth()) {
         if (!$this->_token instanceof Zend_Service_RememberTheMilk_Token) {
             throw new Zend_Service_Exception('Authentication token not set');
         }
         $params['auth_token'] = $this->_token->getToken();
     }
     if (strpos($params['method'], 'rtm.time.') !== 0) {
         $this->_sign($params);
     }
     $time = time();
     $elapsed = $time - $this->_lastRequest;
     if ($elapsed < $this->_throttle) {
         sleep($this->_throttle - $elapsed);
     }
     $this->_lastRequest = $time;
     /**
      * @see Zend_Service_Exception
      */
     require_once 'Zend/Service/Exception.php';
     $response = $this->_rest->restGet('/services/rest/', $params);
     if ($response->isSuccessful()) {
         $body = $response->getBody();
         if ($body === null) {
             throw new Zend_Service_Exception('Service appears to be unavailable');
         }
         $body = Zend_Json::decode($body, Zend_Json::TYPE_OBJECT);
         $body = $body->rsp;
         if ($body->stat == 'fail') {
             throw new Zend_Service_Exception($body->err->msg, $body->err->code);
         } else {
             if (isset($body->transaction) && isset($body->transaction->undoable) && $body->transaction->undoable == '1') {
                 $this->_transactions[] = $body->transaction->id;
             }
         }
         return $body;
     }
     throw new Zend_Service_Exception('HTTP ' . $response->getStatus());
 }
 /**
  * 
  * @return void
  */
 public function setUp()
 {
     Zend_Service_Rest::getHttpClient()->setConfig(array('useragent' => 'Zend_Service_Delicious - Unit tests/0.01', 'keepalive' => true));
     $this->_delicious = new Zend_Service_Delicious(self::TEST_UNAME, self::TEST_PASS);
 }