/** * 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 * 'region' => The regional search engine on which the service performs the search. default us. * * @param string $query the query being run * @param array $options any optional parameters * @return Zend_Service_Yahoo_WebResultSet The return set * @throws Zend\Service\Exception */ public function webSearch($query, array $options = array()) { static $defaultOptions = array('type' => 'all', 'start' => 1, 'results' => 10, 'format' => 'any'); $options = $this->_prepareOptions($query, $options, $defaultOptions); $this->_validateWebSearch($options); $this->_rest->getHttpClient()->resetParameters(); $this->_rest->setUri('http://search.yahooapis.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); }
/** * Look up item(s) by ASIN * * @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|Zend_Service_Amazon_ResultSet */ public function itemLookup($asin, array $options = array()) { $defaultOptions = array('IdType' => 'ASIN', 'ResponseGroup' => 'Small'); $options['ItemId'] = (string) $asin; $options = $this->_prepareOptions('ItemLookup', $options, $defaultOptions); $this->_rest->getHttpClient()->resetParameters(); $response = $this->_rest->restGet('/onca/xml', $options); if ($response->isError()) { /** * @see Zend_Service_Exception */ require_once 'Zend/Service/Exception.php'; 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) { /** * @see Zend_Service_Amazon_Item */ require_once 'Zend/Service/Amazon/Item.php'; return new Zend_Service_Amazon_Item($items->item(0)); } /** * @see Zend_Service_Amazon_ResultSet */ require_once 'Zend/Service/Amazon/ResultSet.php'; return new Zend_Service_Amazon_ResultSet($dom); }
/** * Get recommendations from recommender API * @param string $type * @param array $parameters * @return array */ public function getRecommendations($type, $parameters) { try { $response = $this->_client->restGet(sprintf('/recommend/%s', $type), $parameters); $this->_recommendLogger->logApiCall($this->_client, $response); if ($response->getStatus() !== 200) { throw new \Zend_Http_Client_Exception('Recommender failed to respond'); } $recommendations = json_decode($response->getRawBody(), true); if (is_array($recommendations)) { return $recommendations; } } catch (\Zend_Http_Client_Exception $e) { $this->_recommendLogger->logApiCallException($this->_client, $e); } return []; }
public function testRestFixesPathWithMissingSlashes() { $expXml = file_get_contents($this->path . 'returnString.xml'); $response = "HTTP/1.0 200 OK\r\n" . "X-powered-by: PHP/5.2.0\r\n" . "Content-type: text/xml\r\n" . "Content-length: " . strlen($expXml) . "\r\n" . "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n" . "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n" . "Connection: close\r\n" . "\r\n" . $expXml; $this->adapter->setResponse($response); $rest = new Zend_Rest_Client('http://framework.zend.com'); $response = $rest->restGet('rest'); $this->assertTrue($response instanceof Zend_Http_Response); $this->assertContains($expXml, $response->getBody()); }
/** * 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; }
/** * 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 * 'region' => The regional search engine on which the service performs the search. default us. * * @param string $query the query being run * @param array $options any optional parameters * @return Zend_Service_Yahoo_WebResultSet The return set * @throws Zend_Service_Exception */ public function webSearch($query, array $options = array()) { static $defaultOptions = array('type' => 'all', 'start' => 1, 'results' => 10, 'format' => 'any'); $options = $this->_prepareOptions($query, $options, $defaultOptions); $this->_validateWebSearch($options); $this->_rest->getHttpClient()->resetParameters(); $this->_rest->setUri('http://search.yahooapis.com'); $response = $this->_rest->restGet('/WebSearchService/V1/webSearch', $options); if ($response->isError()) { /** * @see Zend_Service_Exception */ require_once LIB_DIR . '/Zend/Service/Exception.php'; throw new Zend_Service_Exception('An error occurred sending request. Status code: ' . $response->getStatus()); } $dom = new DOMDocument(); $dom = Zend_Xml_Security::scan($response->getBody(), $dom); self::_checkErrors($dom); /** * @see Zend_Service_Yahoo_WebResultSet */ require_once LIB_DIR . '/Zend/Service/Yahoo/WebResultSet.php'; return new Zend_Service_Yahoo_WebResultSet($dom); }
/** * 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|Zend_Service_Amazon_ResultSet|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)); } elseif ($items->length > 1) { return new Zend_Service_Amazon_ResultSet($items); } return null; }
require_once 'Zend/Rest/Client.php'; require_once 'Zend/Debug.php'; // client $client = new Zend_Rest_Client('http://localhost:5984'); // compose document $document = array(); $document['title'] = 'document title'; $document['content'] = 'document content'; // JSON-ize document $document = Zend_Json::encode($document); // add document (use POST here) $response = $client->restPost('test', $document); // print response Zend_Debug::dump($response); // get document $response = $client->restGet('test/c447a8366d74d880f35720d92d68419e'); // un-Json-ize document $document = Zend_Json::decode($response->getBody()); // print document Zend_Debug::dump($document); // change values $document['title'] = '"updated title!"'; // update document $response = $client->restPut('test/c447a8366d74d880f35720d92d68419e', Zend_Json::encode($document)); // print response Zend_Debug::dump($response); // get all documents $response = $client->restGet('test/_all_docs'); // print all docs Zend_Debug::dump($response); // next: views
/** * Handles all GET requests to a web service * * @param string $method Requested API method * @param array $params Array of GET parameters * @return mixed decoded response from web service * @throws Bgy_Service_Geonames_Exception */ public function makeRequest($method, $params = array()) { $this->_rest->setUri(self::API_URI); $path = $method; $type = self::$_supportedMethods[$path]['output']; // Construct the path accordingly to the output type switch ($type) { case 'json': $path = $path . 'JSON'; break; case 'xml': $params += array('type' => 'xml'); break; default: /** * @see Bgy_Service_Geonames_Exception */ require_once 'Bgy/Service/Geonames/Exception.php'; throw new Bgy_Service_Geonames_Exception('Unknown request type'); } if (null !== $this->getUsername()) { $params['username'] = $this->getUsername(); } if (null !== $this->getToken()) { $params['token'] = $this->getToken(); } $response = $this->_rest->restGet($path, $params); if (!$response->isSuccessful()) { /** * @see Bgy_Service_Geonames_Exception */ require_once 'Bgy/Service/Geonames/Exception.php'; throw new Bgy_Service_Geonames_Exception("Http client reported an error: '{$response->getMessage()}'"); } $responseBody = $response->getBody(); switch ($type) { case 'xml': $dom = new DOMDocument(); if (!@$dom->loadXML($responseBody)) { /** * @see Bgy_Service_Geonames_Exception */ require_once 'Bgy/Service/Geonames/Exception.php'; throw new Bgy_Service_Geonames_Exception('Malformed XML'); } $jsonResult = Zend_Json::fromXml($dom->saveXML()); break; case 'json': $jsonResult = $responseBody; break; } $arrayFromJson = Zend_Json::decode($jsonResult); if (isset(self::$_supportedMethods[$method]['root']) && null !== ($root = self::$_supportedMethods[$method]['root']) && isset($arrayFromJson[$root])) { $arrayFromJson = $arrayFromJson[$root]; } return $arrayFromJson; }
/** * check whether the bar is active * * @return array */ public function getActiveState() { $csConfig = Mage::getModel("commercesciences_base/config")->load("1"); if (!$csConfig) { //we must be already at least on step 1, so the DB record has to exist Mage::log("Error - no csConfig"); return array('error' => $this->__("Error ocurred. Your updates weren't saved. Please contact ComemrceScience for support (error id: 005)")); } Mage::log("csConfig=" . print_r($csConfig, true), true); // TODO Ron Gross 2/1/2013 - refactor into a method $RESTClient = new Zend_Rest_Client($csConfig->getCsApiUrl()); $httpClient = $RESTClient->getHttpClient(); $httpClient->setConfig(array("timeout" => 30)); try { $response = $RESTClient->restGet("/magento/getBarStatus", array('userID' => $csConfig->getUserId(), 'securityToken' => $csConfig->getSecurityToken())); $responseJson = $response->getBody(); $parsedResponseArr = $this->stdObject2Array(json_decode($responseJson)); if (!isset($parsedResponseArr['good'])) { Mage::log("Server busy"); return array('error' => $this->__("The CommerceSciences server is currently busy, your updates weren't saved. Please try again later. (error id: 006)")); } if ($parsedResponseArr['good'] == false) { if (isset($parsedResponseArr['fieldErrors']) && $parsedResponseArr['fieldErrors']) { $fieldErrorsArr = $this->stdObject2Array($parsedResponseArr['fieldErrors']); $errorMsg = ''; foreach ($fieldErrorsArr as $field => $fError) { $errorMsg .= "<br />"; $errorMsg .= $this->__($field) . ": " . $this->__($fError); } $errorMsg = substr($errorMsg, 0, strlen($errorMsg) - 1); Mage::log("Error (fieldErrors) - " . $errorMsg); return array('error' => $errorMsg); } elseif (isset($parsedResponseArr['globalError']) && $parsedResponseArr['globalError']) { Mage::log("Error (globalError) - " . $parsedResponseArr['globalError']); return array('error' => $parsedResponseArr['globalError']); } } Mage::log("Returning data: " . $parsedResponseArr['data']); return array('error' => false, 'data' => $parsedResponseArr['data']); } catch (Exception $e) { Mage::log("Got error: " . print_r($e, true)); return array('error' => $this->__($e->getMessage())); } }