PageRenderTime 40ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Service/Ebay/Finding.php

https://bitbucket.org/hjain/loudmusic
PHP | 421 lines | 184 code | 43 blank | 194 comment | 16 complexity | 4f781b20068fc0f494650c9fbb863831 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Service
  17. * @subpackage Ebay
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Finding.php 22824 2010-08-09 18:59:54Z renanbr $
  21. */
  22. /**
  23. * @see Zend_Service_Ebay_Abstract
  24. */
  25. require_once 'Zend/Service/Ebay/Abstract.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Service
  29. * @subpackage Ebay
  30. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. * @uses Zend_Service_Ebay_Abstract
  33. */
  34. class Zend_Service_Ebay_Finding extends Zend_Service_Ebay_Abstract
  35. {
  36. const SERVICE_NAME = 'FindingService';
  37. const SERVICE_VERSION = '1.0.0';
  38. const RESPONSE_DATA_FORMAT = 'XML';
  39. const ENDPOINT_URI = 'http://svcs.ebay.com';
  40. const ENDPOINT_PATH = 'services/search/FindingService/v1';
  41. const XMLNS_FINDING = 'e';
  42. const XMLNS_MS = 'ms';
  43. /**
  44. * @var array
  45. */
  46. protected static $_xmlNamespaces = array(
  47. self::XMLNS_FINDING => 'http://www.ebay.com/marketplace/search/v1/services',
  48. self::XMLNS_MS => 'http://www.ebay.com/marketplace/services'
  49. );
  50. /**
  51. *
  52. * @var array
  53. */
  54. protected $_options = array(
  55. self::OPTION_GLOBAL_ID => 'EBAY-US'
  56. );
  57. /**
  58. * @return array
  59. */
  60. public static function getXmlNamespaces()
  61. {
  62. return self::$_xmlNamespaces;
  63. }
  64. /**
  65. * @param Zend_Config|array|string $options Application Id or array of options
  66. * @throws Zend_Service_Ebay_Finding_Exception When application id is missing
  67. * @return void
  68. */
  69. public function __construct($options)
  70. {
  71. // prepare options
  72. if (is_string($options)) {
  73. // application id was given
  74. $options = array(self::OPTION_APP_ID => $options);
  75. } else {
  76. // check application id
  77. $options = parent::optionsToArray($options);
  78. if (!array_key_exists(self::OPTION_APP_ID, $options)) {
  79. /**
  80. * @see Zend_Service_Ebay_Finding_Exception
  81. */
  82. require_once 'Zend/Service/Ebay/Finding/Exception.php';
  83. throw new Zend_Service_Ebay_Finding_Exception(
  84. 'Application Id is missing.');
  85. }
  86. }
  87. // load options
  88. parent::setOption($options);
  89. }
  90. /**
  91. * @param Zend_Rest_Client $client
  92. * @return Zend_Service_Ebay_Finding Provides a fluent interface
  93. */
  94. public function setClient($client)
  95. {
  96. if (!$client instanceof Zend_Rest_Client) {
  97. /**
  98. * @see Zend_Service_Ebay_Finding_Exception
  99. */
  100. require_once 'Zend/Service/Ebay/Finding/Exception.php';
  101. throw new Zend_Service_Ebay_Finding_Exception(
  102. 'Client object must extend Zend_Rest_Client.');
  103. }
  104. $this->_client = $client;
  105. return $this;
  106. }
  107. /**
  108. * @return Zend_Rest_Client
  109. */
  110. public function getClient()
  111. {
  112. if (!$this->_client instanceof Zend_Rest_Client) {
  113. /**
  114. * @see Zend_Rest_Client
  115. */
  116. require_once 'Zend/Rest/Client.php';
  117. $this->_client = new Zend_Rest_Client();
  118. }
  119. return $this->_client;
  120. }
  121. /**
  122. * Finds items by a keyword query and/or category and allows searching
  123. * within item descriptions.
  124. *
  125. * @param string $keywords
  126. * @param boolean $descriptionSearch
  127. * @param integer $categoryId
  128. * @param Zend_Config|array $options
  129. * @link http://developer.ebay.com/DevZone/finding/CallRef/findItemsAdvanced.html
  130. * @return Zend_Service_Ebay_Finding_Response_Items
  131. */
  132. public function findItemsAdvanced($keywords, $descriptionSearch = true, $categoryId = null, $options = null)
  133. {
  134. // prepare options
  135. $options = parent::optionsToArray($options);
  136. $options['keywords'] = $keywords;
  137. $options['descriptionSearch'] = $descriptionSearch;
  138. if (!empty($categoryId)) {
  139. $options['categoryId'] = $categoryId;
  140. }
  141. // do request
  142. return $this->_findItems($options, 'findItemsAdvanced');
  143. }
  144. /**
  145. * Finds items in a specific category. Results can be filtered and sorted.
  146. *
  147. * @param integer $categoryId
  148. * @param Zend_Config|array $options
  149. * @link http://developer.ebay.com/DevZone/finding/CallRef/findItemsByCategory.html
  150. * @return Zend_Service_Ebay_Finding_Response_Items
  151. */
  152. public function findItemsByCategory($categoryId, $options = null)
  153. {
  154. // prepare options
  155. $options = parent::optionsToArray($options);
  156. $options['categoryId'] = $categoryId;
  157. // do request
  158. return $this->_findItems($options, 'findItemsByCategory');
  159. }
  160. /**
  161. * Finds items on eBay based upon a keyword query and returns details for
  162. * matching items.
  163. *
  164. * @param string $keywords
  165. * @param Zend_Config|array $options
  166. * @link http://developer.ebay.com/DevZone/finding/CallRef/findItemsByKeywords.html
  167. * @return Zend_Service_Ebay_Finding_Response_Items
  168. */
  169. public function findItemsByKeywords($keywords, $options = null)
  170. {
  171. // prepare options
  172. $options = parent::optionsToArray($options);
  173. $options['keywords'] = $keywords;
  174. // do request
  175. return $this->_findItems($options, 'findItemsByKeywords');
  176. }
  177. /**
  178. * Finds items based upon a product ID, such as an ISBN, UPC, EAN, or ePID.
  179. *
  180. * @param integer $productId
  181. * @param string $productIdType Default value is ReferenceID
  182. * @param Zend_Config|array $options
  183. * @link http://developer.ebay.com/DevZone/finding/CallRef/findItemsByProduct.html
  184. * @return Zend_Service_Ebay_Finding_Response_Items
  185. */
  186. public function findItemsByProduct($productId, $productIdType = null, $options = null)
  187. {
  188. if (null == $productIdType) {
  189. $productIdType = 'ReferenceID';
  190. }
  191. // prepare options
  192. $options = parent::optionsToArray($options);
  193. $options['productId'] = array('' => $productId,
  194. 'type' => $productIdType);
  195. // do request
  196. return $this->_findItems($options, 'findItemsByProduct');
  197. }
  198. /**
  199. * Finds items in eBay stores. Can search a specific store or can search all
  200. * stores with a keyword query.
  201. *
  202. * @param string $storeName
  203. * @param Zend_Config|array $options
  204. * @link http://developer.ebay.com/DevZone/finding/CallRef/findItemsIneBayStores.html
  205. * @return Zend_Service_Ebay_Finding_Response_Items
  206. */
  207. public function findItemsInEbayStores($storeName, $options = null)
  208. {
  209. // prepare options
  210. $options = parent::optionsToArray($options);
  211. $options['storeName'] = $storeName;
  212. // do request
  213. return $this->_findItems($options, 'findItemsIneBayStores');
  214. }
  215. /**
  216. * @param array $options
  217. * @param string $operation
  218. * @return Zend_Service_Ebay_Finding_Response_Items
  219. */
  220. protected function _findItems(array $options, $operation)
  221. {
  222. // set default output selector value
  223. if (!array_key_exists('outputSelector', $options)) {
  224. $options['outputSelector'] = array('AspectHistogram',
  225. 'CategoryHistogram',
  226. 'SellerInfo',
  227. 'StoreInfo');
  228. }
  229. // do request
  230. $dom = $this->_request($operation, $options);
  231. /**
  232. * @see Zend_Service_Ebay_Finding_Response_Items
  233. */
  234. require_once 'Zend/Service/Ebay/Finding/Response/Items.php';
  235. $response = new Zend_Service_Ebay_Finding_Response_Items($dom->firstChild);
  236. return $response->setOperation($operation)
  237. ->setOption($options);
  238. }
  239. /**
  240. * Gets category and/or aspect metadata for the specified category.
  241. *
  242. * @param integer $categoryId
  243. * @param Zend_Config|array $options
  244. * @link http://developer.ebay.com/DevZone/finding/CallRef/getHistograms.html
  245. * @return Zend_Service_Ebay_Finding_Response_Histograms
  246. */
  247. public function getHistograms($categoryId, $options = null)
  248. {
  249. // prepare options
  250. $options = parent::optionsToArray($options);
  251. $options['categoryId'] = $categoryId;
  252. // do request
  253. $operation = 'getHistograms';
  254. $dom = $this->_request($operation, $options);
  255. /**
  256. * @see Zend_Service_Ebay_Finding_Response_Histograms
  257. */
  258. require_once 'Zend/Service/Ebay/Finding/Response/Histograms.php';
  259. $response = new Zend_Service_Ebay_Finding_Response_Histograms($dom->firstChild);
  260. return $response->setOperation($operation)
  261. ->setOption($options);
  262. }
  263. /**
  264. * Checks specified keywords and returns correctly spelled keywords for best
  265. * search results.
  266. *
  267. * @param string $keywords
  268. * @param Zend_Config|array $options
  269. * @link http://developer.ebay.com/DevZone/finding/CallRef/getSearchKeywordsRecommendation.html
  270. * @return Zend_Service_Ebay_Finding_Response_Keywords
  271. */
  272. public function getSearchKeywordsRecommendation($keywords, $options = null)
  273. {
  274. // prepare options
  275. $options = parent::optionsToArray($options);
  276. $options['keywords'] = $keywords;
  277. // do request
  278. $operation = 'getSearchKeywordsRecommendation';
  279. $dom = $this->_request($operation, $options);
  280. /**
  281. * @see Zend_Service_Ebay_Finding_Response_Keywords
  282. */
  283. require_once 'Zend/Service/Ebay/Finding/Response/Keywords.php';
  284. $response = new Zend_Service_Ebay_Finding_Response_Keywords($dom->firstChild);
  285. return $response->setOperation($operation)
  286. ->setOption($options);
  287. }
  288. /**
  289. * @param string $operation
  290. * @param array $options
  291. * @link http://developer.ebay.com/DevZone/finding/Concepts/MakingACall.html#StandardURLParameters
  292. * @return DOMDocument
  293. */
  294. protected function _request($operation, array $options = null)
  295. {
  296. // generate default options
  297. // constructor load global-id and application-id values
  298. $default = array('OPERATION-NAME' => $operation,
  299. 'SERVICE-NAME' => self::SERVICE_NAME,
  300. 'SERVICE-VERSION' => self::SERVICE_VERSION,
  301. 'GLOBAL-ID' => $this->getOption(self::OPTION_GLOBAL_ID),
  302. 'SECURITY-APPNAME' => $this->getOption(self::OPTION_APP_ID),
  303. 'RESPONSE-DATA-FORMAT' => self::RESPONSE_DATA_FORMAT,
  304. 'REST-PAYLOAD' => '');
  305. // prepare options to ebay syntax
  306. $options = $default + $this->_optionsToNameValueSyntax($options);
  307. // do request
  308. $client = $this->getClient();
  309. $client->getHttpClient()->resetParameters();
  310. $response = $client->setUri(self::ENDPOINT_URI)
  311. ->restGet(self::ENDPOINT_PATH, $options);
  312. return $this->_parseResponse($response);
  313. }
  314. /**
  315. * Search for error from request.
  316. *
  317. * If any error is found a DOMDocument is returned, this object contains a
  318. * DOMXPath object as "ebayFindingXPath" attribute.
  319. *
  320. * @param Zend_Http_Response $response
  321. * @link http://developer.ebay.com/DevZone/finding/CallRef/types/ErrorSeverity.html
  322. * @see Zend_Service_Ebay_Finding_Abstract::_initXPath()
  323. * @throws Zend_Service_Ebay_Finding_Exception When any error occurrs during request
  324. * @return DOMDocument
  325. */
  326. protected function _parseResponse(Zend_Http_Response $response)
  327. {
  328. // error message
  329. $message = '';
  330. // first trying, loading XML
  331. $dom = new DOMDocument();
  332. if (!@$dom->loadXML($response->getBody())) {
  333. $message = 'It was not possible to load XML returned.';
  334. }
  335. // second trying, check request status
  336. if ($response->isError()) {
  337. $message = $response->getMessage()
  338. . ' (HTTP status code #' . $response->getStatus() . ')';
  339. }
  340. // third trying, search for error message into XML response
  341. // only first error that contains severiry=Error is read
  342. $xpath = new DOMXPath($dom);
  343. foreach (self::$_xmlNamespaces as $alias => $uri) {
  344. $xpath->registerNamespace($alias, $uri);
  345. }
  346. $ns = self::XMLNS_FINDING;
  347. $nsMs = self::XMLNS_MS;
  348. $expression = "//$nsMs:errorMessage[1]/$ns:error/$ns:severity[.='Error']";
  349. $severityNode = $xpath->query($expression)->item(0);
  350. if ($severityNode) {
  351. $errorNode = $severityNode->parentNode;
  352. // ebay message
  353. $messageNode = $xpath->query("//$ns:message[1]", $errorNode)->item(0);
  354. if ($messageNode) {
  355. $message = 'eBay error: ' . $messageNode->nodeValue;
  356. } else {
  357. $message = 'eBay error: unknown';
  358. }
  359. // ebay error id
  360. $errorIdNode = $xpath->query("//$ns:errorId[1]", $errorNode)->item(0);
  361. if ($errorIdNode) {
  362. $message .= ' (#' . $errorIdNode->nodeValue . ')';
  363. }
  364. }
  365. // throw exception when an error was detected
  366. if (strlen($message) > 0) {
  367. /**
  368. * @see Zend_Service_Ebay_Finding_Exception
  369. */
  370. require_once 'Zend/Service/Ebay/Finding/Exception.php';
  371. throw new Zend_Service_Ebay_Finding_Exception($message);
  372. }
  373. // add xpath to dom document
  374. // it allows service_ebay_finding classes use this
  375. $dom->ebayFindingXPath = $xpath;
  376. return $dom;
  377. }
  378. }