예제 #1
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
     */
    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) {
            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 '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 '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 'Zend/Service/Delicious/Exception.php';
                    throw new Zend_Service_Delicious_Exception('XML Error');
                }

                return $dom;
            case 'json':
                return Zend_Json_Decoder::decode($responseBody);
        }
    }
예제 #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 DOMDocument response from web service
  */
 public function makeRequest($path, $parms = array(), $type = 'xml')
 {
     settype($parms, 'array');
     // 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) {
         usleep((1 - $timeDiff) * 1000000);
     }
     $this->_http->resetParameters();
     foreach ($parms as $f_parm => $f_value) {
         $this->_http->setParameterGet($f_parm, $f_value);
     }
     switch ($type) {
         case 'xml':
             $this->_http->setUri(self::API_URI . $path);
             break;
         case 'json':
             $this->_http->setUri(self::JSON_URI . $path);
             $this->_http->setParameterGet('raw', true);
             break;
         default:
             throw new Zend_Service_Delicious_Exception('Unknown request type');
     }
     self::$_lastRequestTime = microtime(true);
     $response = $this->_http->request(Zend_Http_Client::GET);
     if (!$response->isSuccessful()) {
         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)) {
                 throw new Zend_Service_Delicious_Exception('XML Error');
             }
             return $dom;
         case 'json':
             return Zend_Json::decode($responseBody);
     }
 }