PluginProbe
Datafeedr API / 1.0.15
Datafeedr API v1.0.15
1.4.3 1.4.2 1.0.125 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.2 1.0.20 1.0.21 1.0.22 1.0.23 1.0.24 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.3 1.0.30 1.0.31 1.0.32 All 182 releases
datafeedr-api / libraries / datafeedr.php

datafeedr.php in Datafeedr API 1.0.15, at libraries/datafeedr.php

968 lines 26.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Datafeedr Api Client Library.
5 *
6 * @version 0.1b.6693
7 * @copyright Datafeedr 2007 ~ 2013 - All Rights Reserved
8 *
9 * @mainpage
10 *
11 * Example of use:
12 *
13 * @code
14 * $api = new DatafeedrApi('<access id>', '<secret key>');
15 *
16 * foreach($api->getMerchants() as $merchant)
17 * print $merchant['name'];
18 *
19 * $search = $api->searchRequest();
20 * $search->addFilter("name LIKE shoe");
21 * $search->addFilter("price < 100");
22 * $search->addSort("price", DatafeedrApi::SORT_ASCENDING);
23 * $search->setLimit(25);
24 *
25 * $products = $search->execute();
26 *
27 * foreach($products as $product)
28 * print $product['name'];
29 *
30 * @endcode
31 *
32 *
33 *
34 **/
35
36 if(!class_exists('DatafeedrApi', false)) {
37
38 /**
39 * Datafeedr API core class.
40 **/
41 class DatafeedrApi
42 {
43 protected $_accessId;
44 protected $_secretKey;
45
46 protected $_transport;
47 protected $_timeout;
48 protected $_url;
49 protected $_returnObjects;
50
51 protected $_status;
52
53 const SORT_DESCENDING = -1;
54 const SORT_ASCENDING = +1;
55
56 const DEFAULT_URL = 'http://api.datafeedr.com';
57 const DEFAULT_TIMEOUT = 30;
58
59 const VERSION = '0.1b.6693';
60
61 /**
62 * Constructor.
63 *
64 * @param string $accessId Access ID.
65 * @param object $secretKey Secret key.
66 * @param string|callable $transport (optional) HTTP transport function.
67 * @param int $timeout (optional) HTTP connection timeout, in seconds.
68 * @param bool $returnObjects (optional) if TRUE, responses are objects, otherwise associative arrays.
69 *
70 * The optional $transport parameter tells how HTTP requests should be made.
71 * It can be either a string that describes one of built-in transports ("curl", "file" or "socket"),
72 * or a callable object that should accept an url, an array of headers and a string of post data and
73 * should return an array [int http response status, string response body].
74 *
75 **/
76 public function __construct($accessId, $secretKey, $transport = 'curl', $timeout = 0, $returnObjects = FALSE) {
77 $this->_accessId = $accessId;
78 $this->_secretKey = $secretKey;
79
80 $this->_errors = array(
81 1 => 'DatafeedrBadRequestError',
82 2 => 'DatafeedrAuthenticationError',
83 3 => 'DatafeedrLimitExceededError',
84 4 => 'DatafeedrQueryError',
85 7 => 'DatafeedrExternalError',
86 9 => 'DatafeedrError',
87 );
88
89 $this->_url = self::DEFAULT_URL;
90 $this->_timeout = $timeout ? $timeout : self::DEFAULT_TIMEOUT;
91 $this->_returnObjects = $returnObjects;
92
93 switch($transport) {
94 case 'curl':
95 $this->_transport = array($this, '_transportCurl');
96 break;
97 case 'file':
98 $this->_transport = array($this, '_transportFile');
99 break;
100 case 'socket':
101 $this->_transport = array($this, '_transportSocket');
102 break;
103 default:
104 if(!is_callable($transport)) {
105 throw new DatafeedrError("Transport must be a function");
106 }
107 $this->_transport = $transport;
108 }
109 }
110
111 /**
112 * Return status information.
113 *
114 * @return array
115 *
116 **/
117 public function getStatus() {
118 $this->apiCall('status');
119 return $this->_status;
120 }
121
122 /**
123 * Return status information from the last request.
124 *
125 * If no Api request have been made, return NULL
126 *
127 * @return array|null
128 *
129 **/
130 public function lastStatus() {
131 return $this->_status;
132 }
133
134 /**
135 * Return the list of networks.
136 *
137 * @param int|array $networkId (optional) Network id or an array of network ids
138 * @param bool $includeEmpty (optional) If FALSE, omit networks with 0 products
139 * @param array $fields (optional) list of fields to retrieve
140 * @return array
141 *
142 **/
143 public function getNetworks($networkId = NULL, $includeEmpty = FALSE, $fields = NULL) {
144 $request = array();
145 if($networkId) {
146 $request['_ids'] = $this->_intarray($networkId);
147 }
148 $request['skip_empty'] = intval(!$includeEmpty);
149 if($fields) {
150 $request['fields'] = $fields;
151 }
152 $response = $this->apiCall('networks', $request);
153 return $this->_get($response, 'networks');
154 }
155
156 /**
157 * Return the list of merchants.
158 *
159 * @param int|array $networkId (optional) Network id or array of network ids
160 * @param bool $includeEmpty (optional) If FALSE, omit merchants with 0 products
161 * @param array $fields (optional) list of fields to retrieve
162 * @return array
163 *
164 **/
165 public function getMerchants($networkId = NULL, $includeEmpty = FALSE, $fields = NULL) {
166 $request = array();
167 if($networkId) {
168 $request['source_ids'] = $this->_intarray($networkId);
169 }
170 $request['skip_empty'] = intval(!$includeEmpty);
171 if($fields) {
172 $request['fields'] = $fields;
173 }
174 $response = $this->apiCall('merchants', $request);
175 return $this->_get($response, 'merchants');
176 }
177
178 /**
179 * Return the list of merchants by their ids.
180 *
181 * @param int|array $merchantId Merchant id or array of network ids
182 * @param bool $includeEmpty (optional) If FALSE, omit merchants with 0 products
183 * @param array $fields (optional) list of fields to retrieve
184 * @return array
185 *
186 **/
187 public function getMerchantsById($merchantId, $includeEmpty = FALSE, $fields = NULL) {
188 $request = array();
189 $request['_ids'] = $this->_intarray($merchantId);
190 $request['skip_empty'] = intval(!$includeEmpty);
191 if($fields) {
192 $request['fields'] = $fields;
193 }
194 $response = $this->apiCall('merchants', $request);
195 return $this->_get($response, 'merchants');
196 }
197
198 /**
199 * Return the list of searchable fields.
200 *
201 * @param int|array $networkId (optional) Network id or array of network ids
202 * @return array
203 *
204 **/
205 public function getFields($networkId = NULL) {
206 $request = array();
207 if($networkId) {
208 $request['source_ids'] = $this->_intarray($networkId);
209 }
210 $response = $this->apiCall('fields', $request);
211 return $this->_get($response, 'fields');
212 }
213
214 /**
215 * Return the list of products by their ids.
216 *
217 * @param int|array $productId Product id or an array of products ids.
218 * @param array $fields (optional) list of fields to retrieve.
219 * @return array
220 *
221 **/
222 public function getProducts($productId, $fields = NULL) {
223 $request = array();
224 $request['_ids'] = $this->_intarray($productId);
225 $request['string_ids'] = 1;
226 if($fields) {
227 $request['fields'] = $fields;
228 }
229 $response = $this->apiCall('get', $request);
230 return $this->_get($response, 'products');
231 }
232
233 /**
234 * Return the list of Zanox merchant ids ("zmids").
235 *
236 * @param int|array $merchantId Merchant id or an array of merchant ids.
237 * @param int $adspaceId Zanox adspace Id.
238 * @param string $connectId Zanox connection Id.
239 * @return array
240 *
241 **/
242 public function getZanoxMerchantIds($merchantId, $adspaceId, $connectId) {
243 $request = array();
244 $request['merchant_ids'] = $this->_intarray($merchantId);
245 $request['adspace_id'] = $adspaceId;
246 $request['connect_id'] = $connectId;
247 $response = $this->apiCall('zanox_merchant_ids', $request);
248 return $this->_get($response, 'zanox_merchant_ids');
249 }
250
251 /**
252 * Create a new DatafeedrSearchRequest object.
253 *
254 * @return DatafeedrSearchRequest
255 *
256 **/
257 public function searchRequest() {
258 return new DatafeedrSearchRequest($this);
259 }
260
261 /**
262 * Create a new DatafeedrAmazonSearchRequest object.
263 *
264 * @return DatafeedrAmazonSearchRequest
265 *
266 **/
267 public function amazonSearchRequest($awsAccessKeyId, $awsSecretKey, $awsAssociateTag, $locale="US") {
268 return new DatafeedrAmazonSearchRequest($this, $awsAccessKeyId, $awsSecretKey, $awsAssociateTag, $locale);
269 }
270
271 /**
272 * Create a new DatafeedrAmazonLookupRequest object.
273 *
274 * @return DatafeedrAmazonLookupRequest
275 *
276 **/
277 public function amazonLookupRequest($awsAccessKeyId, $awsSecretKey, $awsAssociateTag, $locale="US") {
278 return new DatafeedrAmazonLookupRequest($this, $awsAccessKeyId, $awsSecretKey, $awsAssociateTag, $locale);
279 }
280
281 /**
282 * Perform the raw Api call.
283 *
284 * @param string $action Api action.
285 * @param array $request (optional) Request data.
286 * @return array
287 *
288 **/
289 public function apiCall($action, $request = NULL) {
290 if(!$request) {
291 $request = array();
292 }
293
294 $request['aid'] = $this->_accessId;
295 $request['timestamp'] = gmdate('Y-m-d H:i:s');
296
297 $message = $request['aid'] .$action . $request['timestamp'];
298 $request['signature'] = hash_hmac('sha256', $message, $this->_secretKey, FALSE);
299
300 $postdata = json_encode($request);
301 $url = $this->_url . '/' . $action;
302 $headers = array(
303 'Content-Type: application/json',
304 'Accept: application/json',
305 'Content-Length: '. strlen($postdata),
306 'Connection: close',
307 'User-Agent: datafeedr.php.' . self::VERSION
308 );
309 list($status, $response) = call_user_func($this->_transport, $url, $headers, $postdata);
310 if(strlen($response)) {
311 $response = json_decode($response, !$this->_returnObjects);
312 }
313
314 $error = $this->_get($response, 'error');
315 if($error) {
316 $type = $this->_get($response, 'type');
317 $cls = isset($this->_errors[$type]) ? $this->_errors[$type] : 'DatafeedrError';
318 throw new $cls($this->_get($response, 'message'), $error);
319 }
320
321 if($status != 200) {
322 throw new DatafeedrHTTPError("Status $status");
323 }
324
325 $this->_status = $this->_get($response, 'status');
326 return $response;
327 }
328
329 protected function _intarray($id_or_ids) {
330 if(is_numeric($id_or_ids)) {
331 return array($id_or_ids);
332 }
333 if(is_array($id_or_ids)) {
334 return $id_or_ids;
335 }
336 return array();
337 }
338
339 protected function _get($obj, $prop, $default=NULL) {
340 if(is_array($obj) && isset($obj[$prop])) {
341 return $obj[$prop];
342 }
343 if(is_object($obj) && isset($obj->$prop)) {
344 return $obj->$prop;
345 }
346 return $default;
347 }
348
349
350 /**
351 * Perform a HTTP post request by the means of the curl library.
352 *
353 * @param string $url Request url.
354 * @param array $headers Array of headers.
355 * @param string $postdata Post data.
356 * @return array (int http status, string response body)
357 *
358 **/
359 protected function _transportCurl($url, $headers, $postdata) {
360 $ch = curl_init();
361 curl_setopt($ch, CURLOPT_URL, $url);
362 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->_timeout);
363 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
364 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
365 curl_setopt($ch, CURLOPT_POST, 1);
366 curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
367
368 $response = curl_exec($ch);
369 $status = intval(curl_getinfo($ch, CURLINFO_HTTP_CODE));
370 $errno = curl_errno($ch);
371 $errmsg = curl_error($ch);
372
373 curl_close($ch);
374
375 if($errno) {
376 throw new DatafeedrHTTPError($errmsg, $errno);
377 }
378
379 return array($status, $response);
380 }
381
382 /**
383 * Perform a HTTP post request using file functions.
384 *
385 * @param string $url Request url.
386 * @param array $headers Array of headers.
387 * @param string $postdata Post data.
388 * @return array (int http status, string response body)
389 *
390 **/
391 protected function _transportFile($url, $headers, $postdata) {
392 $options = array('http' => array(
393 'method' => 'POST',
394 'content' => $postdata,
395 'header' => implode("\r\n", $headers),
396 'ignore_errors' => TRUE,
397 'timeout' => $this->_timeout,
398 ));
399 $context = stream_context_create($options);
400 $response = file_get_contents($url, false, $context);
401
402 $status = 200;
403 if(isset($http_response_header) && isset($http_response_header[0])) {
404 if(preg_match('/HTTP.+?(\d\d\d)/', $http_response_header[0], $match)) {
405 $status = intval($match[1]);
406 }
407 } else if($response === false) {
408 throw new DatafeedrHTTPError("HTTP error: invalid response");
409 }
410 return array($status, $response);
411 }
412
413 /**
414 * Perform a HTTP post request using sockets.
415 *
416 * @param string $url Request url.
417 * @param array $headers Array of headers.
418 * @param string $postdata Post data.
419 * @return array (int http status, string response body)
420 *
421 **/
422 protected function _transportSocket($url, $headers, $postdata) {
423 $parts = parse_url($url);
424 $errno = 0;
425 $errmsg = '';
426
427 $fp = fsockopen($parts['host'], 80, $errno, $errmsg, $this->_timeout);
428 if(!$fp) {
429 throw new DatafeedrHTTPError($errmsg, $errno);
430 }
431
432 fwrite($fp, "POST " . $parts['path'] . " HTTP/1.1\r\n");
433 fwrite($fp, implode("\r\n", $headers) . "\r\n\r\n");
434 fwrite($fp, $postdata);
435
436 $buf = '';
437 while(!feof($fp)) {
438 $buf .= fgets($fp, 1024);
439 }
440 fclose($fp);
441
442 $buf = explode("\r\n\r\n", $buf, 2);
443 if(count($buf) != 2) {
444 throw new DatafeedrHTTPError("Invalid response");
445 }
446 if(preg_match('/HTTP.+?(\d\d\d)/', $buf[0], $match)) {
447 $status = intval($match[1]);
448 } else {
449 throw new DatafeedrHTTPError("Invalid status");
450 }
451 return array($status, $buf[1]);
452 }
453 }
454 }
455
456 if(!class_exists('DatafeedrSearchRequestBase', false)) {
457 /**
458 * Generic Datafeedr API search request.
459 **/
460 class DatafeedrSearchRequestBase
461 {
462 protected $_api;
463 protected $_lastResponse;
464
465 /**
466 * Constructor.
467 *
468 * @param object $api DatafeedrApi object.
469 *
470 **/
471 public function __construct($api) {
472 $this->_api = $api;
473 }
474
475 /**
476 * Get the number of found products.
477 *
478 * @return int
479 *
480 **/
481 public function getFoundCount() {
482 return $this->_responseItem('found_count', 0);
483 }
484
485 /**
486 * Get the number of products that can be retrieved from the server.
487 *
488 * @return int
489 *
490 **/
491 public function getResultCount() {
492 return $this->_responseItem('result_count', 0);
493 }
494
495 /**
496 * Return the response from the last search.
497 *
498 * @return array
499 *
500 **/
501 public function getResponse() {
502 return $this->_lastResponse;
503 }
504
505 protected function _responseItem($prop, $default) {
506 if(is_null($this->_lastResponse)) {
507 throw new DatafeedrError("Reading from an empty request");
508 }
509 if(is_object($this->_lastResponse) && isset($this->_lastResponse->$prop)) {
510 return $this->_lastResponse->$prop;
511 }
512 if(is_array($this->_lastResponse) && isset($this->_lastResponse[$prop])) {
513 return $this->_lastResponse[$prop];
514 }
515 return $default;
516 }
517
518 function _apiCall($action, $request = NULL) {
519 $this->_lastResponse = $this->_api->apiCall($action, $request);
520 }
521
522 }
523 }
524
525 if(!class_exists('DatafeedrSearchRequest', false)) {
526 /**
527 * Search request for Datafeedr API.
528 **/
529 class DatafeedrSearchRequest extends DatafeedrSearchRequestBase
530 {
531 /**
532 * Constructor.
533 *
534 * @param object $api DatafeedrApi object.
535 *
536 **/
537 public function __construct($api) {
538 parent::__construct($api);
539
540 $this->_query = array();
541 $this->_sort = array();
542 $this->_fields = array();
543 $this->_limit = 0;
544 $this->_offset = 0;
545 $this->_priceGroups = 0;
546 $this->_excludeDuplicates = "";
547 }
548
549 /**
550 * Add a query filter.
551 *
552 * @param string $filter Query filter.
553 * @return $this
554 *
555 **/
556 public function addFilter($filter) {
557 $this->_query []= $filter;
558 return $this;
559 }
560
561 /**
562 * Add a sort field.
563 *
564 * @param string $field Field name.
565 * @param int $order One of DatafeedrApi::SORT_ASCENDING or DatafeedrApi::SORT_DESCENDING
566 * @return $this
567 *
568 **/
569 public function addSort($field, $order = DatafeedrApi::SORT_ASCENDING) {
570 if(strlen($field) && ($field[0] == '+' || $field[0] == '-')) {
571 $this->_sort []= $field;
572 } else if($order == DatafeedrApi::SORT_ASCENDING) {
573 $this->_sort []= '+' . $field;
574 } else if($order == DatafeedrApi::SORT_DESCENDING) {
575 $this->_sort []= '-' . $field;
576 } else {
577 throw new DatafeedrError("Invalid sort order");
578 }
579 return $this;
580 }
581
582 /**
583 * Set which fields to retrieve.
584 *
585 * @param array $fields List of field names.
586 * @return $this
587 *
588 **/
589 public function setFields($fields) {
590 $this->_fields = $fields;
591 return $this;
592 }
593
594 /**
595 * Exclude duplicate results.
596 *
597 * @param string $filter Equality filter in form "field1 field2 | field3".
598 * @return $this
599 *
600 **/
601 public function excludeDuplicates($filter) {
602 if(is_array($filter)) {
603 $filter = implode(' ', $filter);
604 }
605 $this->_excludeDuplicates = $filter;
606 return $this;
607 }
608
609 /**
610 * Set a limit.
611 *
612 * @param int $limit The limit.
613 * @return $this
614 *
615 **/
616 public function setLimit($limit) {
617 $this->_limit = $limit;
618 return $this;
619 }
620
621 /**
622 * Set an offset.
623 *
624 * @param int $offset The offset.
625 * @return $this
626 *
627 **/
628 public function setOffset($offset) {
629 $this->_offset = $offset;
630 return $this;
631 }
632
633 /**
634 * Set a price group count.
635 *
636 * @param int $groups Group count.
637 * @return $this
638 *
639 **/
640 public function setPriceGroups($groups) {
641 $this->_priceGroups = $groups;
642 return $this;
643 }
644
645
646 /**
647 * Get found networks.
648 *
649 * @return array
650 *
651 **/
652 public function getNetworks() {
653 return $this->_responseItem('networks', array());
654 }
655
656 /**
657 * Get found merchants.
658 *
659 * @return array
660 *
661 **/
662 public function getMerchants() {
663 return $this->_responseItem('merchants', array());
664 }
665
666 /**
667 * Get found price groups.
668 *
669 * @return array
670 *
671 **/
672 public function getPriceGroups() {
673 return $this->_responseItem('price_groups', array());
674 }
675
676 /**
677 * Create a request object to use with the API.
678 *
679 * @return array
680 *
681 **/
682 public function getParams() {
683 $request = array();
684 if($this->_query) {
685 $request['query'] = $this->_query;
686 }
687 if($this->_sort) {
688 $request['sort'] = $this->_sort;
689 }
690 if($this->_fields) {
691 $request['fields'] = $this->_fields;
692 }
693 if($this->_limit) {
694 $request['limit'] = $this->_limit;
695 }
696 if($this->_offset) {
697 $request['offset'] = $this->_offset;
698 }
699 if($this->_priceGroups) {
700 $request['price_groups'] = $this->_priceGroups;
701 }
702 if($this->_excludeDuplicates) {
703 $request['exclude_duplicates'] = $this->_excludeDuplicates;
704 }
705 $request['string_ids'] = 1;
706 return $request;
707 }
708
709 /**
710 * Run search and return a list of products.
711 *
712 * @return array
713 *
714 **/
715 public function execute() {
716 $params = $this->getParams();
717 if(!isset($params['query'])) {
718 throw new DatafeedrError("Query can't be empty");
719 }
720 $this->_apiCall('search', $params);
721 return $this->_responseItem('products', array());
722 }
723
724 }
725 }
726
727 if(!class_exists('DatafeedrAmazonRequest', false)) {
728 /**
729 * Generic Amazon request.
730 **/
731 class DatafeedrAmazonRequest extends DatafeedrSearchRequestBase
732 {
733 protected $_found = -1;
734
735 const AWS_VERSION = "2011-08-01";
736
737 /**
738 * Constructor.
739 *
740 * @param object $api DatafeedrApi object.
741 *
742 **/
743 public function __construct($api, $awsAccessKeyId, $awsSecretKey, $awsAssociateTag, $locale="US") {
744 parent::__construct($api);
745 $this->_hosts = array(
746 "CA" => "webservices.amazon.ca",
747 "CN" => "webservices.amazon.cn",
748 "DE" => "webservices.amazon.de",
749 "ES" => "webservices.amazon.es",
750 "FR" => "webservices.amazon.fr",
751 "IT" => "webservices.amazon.it",
752 "JP" => "webservices.amazon.co.jp",
753 "UK" => "webservices.amazon.co.uk",
754 "US" => "webservices.amazon.com",
755 );
756
757 $this->_params = array();
758 $this->_locale = strtoupper($locale);
759
760 if(!isset($this->_hosts[$this->_locale])) {
761 throw new DatafeedrError("Invalid Amazon locale");
762 }
763
764 $this->_awsAccessKeyId = $awsAccessKeyId;
765 $this->_awsSecretKey = $awsSecretKey;
766 $this->_awsAssociateTag = $awsAssociateTag;
767 }
768
769 /**
770 * Returns all parameters.
771 *
772 * @return array
773 *
774 **/
775 public function getParams() {
776 return $this->_params;
777 }
778
779 protected function _amazonUrl($operation, $params, $defaults=NULL) {
780 $params = array_filter($params);
781
782 if(!is_null($defaults)) {
783 foreach($defaults as $k => $v) {
784 if(!isset($params[$k])) {
785 $params[$k] = $v;
786 }
787 }
788 }
789
790 $params["Operation"] = $operation;
791 $params["Service"] = "AWSECommerceService";
792 $params["AWSAccessKeyId"] = $this->_awsAccessKeyId;
793 $params["AssociateTag"] = $this->_awsAssociateTag;
794 $params["Version"] = self::AWS_VERSION;
795 $params["Timestamp"] = gmdate("Y-m-d\\TH:i:s\\Z");
796
797 ksort($params);
798 $query = array();
799 foreach($params as $k => $v) {
800 if(is_array($v)) {
801 $v = implode(',', $v);
802 }
803 $query []= $k . '=' . rawurlencode($v);
804 }
805 $query = implode('&', $query);
806 $host = $this->_hosts[$this->_locale];
807 $path = "/onca/xml";
808 $subj = sprintf("GET\n%s\n%s\n%s", $host, $path, $query);
809 $sign = rawurlencode(base64_encode(hash_hmac("sha256", $subj, $this->_awsSecretKey, TRUE)));
810 return "http://{$host}{$path}?{$query}&Signature={$sign}";
811 }
812 }
813 }
814
815 if(!class_exists('DatafeedrAmazonSearchRequest', false)) {
816 /**
817 * Amazon search request.
818 **/
819 class DatafeedrAmazonSearchRequest extends DatafeedrAmazonRequest
820 {
821 /**
822 * Add a parameter.
823 *
824 * @param string $name Parameter name.
825 * @param string $value Parameter value.
826 * @return $this
827 *
828 * @see http://docs.aws.amazon.com/AWSECommerceService/latest/DG/ItemSearch.html
829 *
830 **/
831 public function addParam($name, $value) {
832 $this->_params[$name] = $value;
833 return $this;
834 }
835
836 /**
837 * Run search and return a list of products.
838 *
839 * @return array
840 *
841 **/
842 public function execute() {
843 $defaults = array(
844 'ResponseGroup' => 'ItemAttributes,Images,OfferFull,BrowseNodes,EditorialReview,VariationSummary',
845 'SearchIndex' => 'All',
846 );
847 $url = $this->_amazonUrl('ItemSearch', $this->_params, $defaults);
848 $this->_apiCall('amazon_search', array('url' => $url));
849 return $this->_responseItem('products', array());
850 }
851 }
852 }
853
854 if(!class_exists('DatafeedrAmazonLookupRequest', false)) {
855 /**
856 * Amazon lookup request.
857 **/
858 class DatafeedrAmazonLookupRequest extends DatafeedrAmazonRequest
859 {
860 /**
861 * Add a parameter.
862 *
863 * @param string $name Parameter name - one of 'ASIN', 'SKU', 'UPC', 'EAN', 'ISBN'
864 * @param string|array $value Parameter value or an array of values (up to 10).
865 * @return $this
866 *
867 * @see http://docs.aws.amazon.com/AWSECommerceService/latest/DG/ItemLookup.html
868 *
869 **/
870 public function addParam($name, $value) {
871 $this->_params[$name] = $value;
872 return $this;
873 }
874
875 /**
876 * Run search and return a list of products.
877 *
878 * @return array
879 *
880 **/
881 public function execute() {
882 $params = array_filter($this->_params);
883 $types = array('ASIN', 'SKU', 'UPC', 'EAN', 'ISBN', 'asin', 'sku', 'upc', 'ean', 'isbn');
884 foreach($types as $type) {
885 if(isset($params[$type])) {
886 $params['IdType'] = strtoupper($type);
887 $params['ItemId'] = $params[$type];
888 unset($params[$type]);
889 }
890 }
891
892 $defaults = array(
893 'ResponseGroup' => 'ItemAttributes,Images,OfferFull,BrowseNodes,EditorialReview,VariationSummary',
894 );
895 if(isset($params['IdType']) && $params['IdType'] != 'ASIN') {
896 $defaults['SearchIndex'] = 'All';
897 }
898 $url = $this->_amazonUrl('ItemLookup', $params, $defaults);
899 $this->_apiCall('amazon_search', array('url' => $url));
900 return $this->_responseItem('products', array());
901 }
902 }
903 }
904
905 if(!class_exists('DatafeedrError', false)) {
906 /**
907 * Generic Api error.
908 **/
909 class DatafeedrError extends Exception
910 {
911 }
912 }
913
914 if(!class_exists('DatafeedrBadRequestError', false)) {
915 /**
916 * API error: Invalid Request.
917 **/
918 class DatafeedrBadRequestError extends DatafeedrError
919 {
920 }
921 }
922
923 if(!class_exists('DatafeedrAuthenticationError', false)) {
924 /**
925 * API error: Authentication failed.
926 **/
927 class DatafeedrAuthenticationError extends DatafeedrError
928 {
929 }
930 }
931
932 if(!class_exists('DatafeedrLimitExceededError', false)) {
933 /**
934 * API error: Query limit exceeded.
935 **/
936 class DatafeedrLimitExceededError extends DatafeedrError
937 {
938 }
939 }
940
941 if(!class_exists('DatafeedrHTTPError', false)) {
942 /**
943 * API error: Unspecified HTTP error.
944 **/
945 class DatafeedrHTTPError extends DatafeedrError
946 {
947 }
948 }
949
950 if(!class_exists('DatafeedrQueryError', false)) {
951 /**
952 * API error: Error in the search query.
953 **/
954 class DatafeedrQueryError extends DatafeedrError
955 {
956 }
957 }
958
959 if(!class_exists('DatafeedrExternalError', false)) {
960 /**
961 * API error: External service error.
962 **/
963 class DatafeedrExternalError extends DatafeedrError
964 {
965 }
966 }
967
968 ?>