/** * 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 $params = 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->httpClient->setAuth($this->authUname, $this->authPass); $this->httpClient->setOptions(array('ssltransport' => 'ssl')); $request = new HttpRequest(); $request->setMethod(HttpRequest::METHOD_GET); switch ($type) { case 'xml': $request->setUri(self::API_URI); break; case 'json': $params['raw'] = true; $request->setUri(self::JSON_URI); break; default: throw new Exception('Unknown request type'); } self::$lastRequestTime = microtime(true); $request->getQuery()->fromArray($params); $response = $this->httpClient->send($request); if (!$response->isSuccess()) { throw new Exception("Http client reported an error: '{$response->getReasonPhrase()}'"); } $responseBody = $response->getBody(); switch ($type) { case 'xml': $dom = new \DOMDocument(); if (!@$dom->loadXML($responseBody)) { throw new Exception('XML Error'); } return $dom; case 'json': return \Zend\Json\Decoder::decode($responseBody); } }
/** * 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) { 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: throw new Exception('Unknown request type'); } self::$_lastRequestTime = microtime(true); $response = $this->_rest->restGet($path, $parms); if (!$response->isSuccessful()) { throw new Exception("Http client reported an error: '{$response->getMessage()}'"); } $responseBody = $response->getBody(); switch ($type) { case 'xml': $dom = new \DOMDocument(); if (!@$dom->loadXML($responseBody)) { throw new Exception('XML Error'); } return $dom; case 'json': return \Zend\Json\Decoder::decode($responseBody); } }