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