Bin will be removed on June 1, 2014 in favor of http://gist.github.com. Please save all your pastes before then. So long, and thanks for all the fish!×
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
<?php
/**
 * A CakePHP datasource for interacting with the amazon eCommerce API.
 *
 * Copyright 2008, Debuggable, Ltd.
 * Hibiskusweg 26c
 * 13089 Berlin, Germany
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright Copyright 2008, Debuggable, Ltd.
 * @version 0.1
 * @author Felix Geisendörfer <felix@debuggable.com>, Tim Koschützki <tim@debuggable.com>
 * @license http://www.opensource.org/licenses/mit-license.php The MIT License
 * @subversion $Id: amazon_ecommerce_source.php 1522 2009-06-11 09:35:43Z charlie $
 */
App::import('Xml');
class AmazonEcommerceSource extends DataSource {
    var $description = "Amazon eCommerce Data Source";
 
    /**
     * List of valid finder method options, supplied as the first parameter to find().
     *
     * @var array
     * @access protected
     */
    var $_findMethods = array(
        'all' => true, 'first' => true, 'count' => true
    );
   
    var $Http = null;
 
    /**
     * Constructor
     *
     * @param string $config
     * @access public
     * @author primeminister
     */
    function __construct($config) {
        parent::__construct($config);
        App::import('HttpSocket');
        $this->Http = new HttpSocket();
    }
 
    /**
     * find items or item on amazon eCommerce API
     *
     * @param mixed $type Find by method (first / all) or just the query (method defaults to all)
     * @param mixed $query string or array of search options
     * @access public
     * @return array Array of records
     * @author primeminister
     */
    function find($type, $query = array()) {
        if (!is_string($type) || (is_string($type) && !array_key_exists($type, $this->_findMethods))) {
            $query = $type;
            $type = 'all';
        }
        if (!is_array($query)) {
            $query = array('Keywords' => $query);
        }
        // setup operation
        if ($type == 'first') {
            $query['Operation'] = 'ItemLookup';
        } else {
            $query['Operation'] = 'ItemSearch';
        }
        // re-map shortcut parameters to the new AWS parameters
        $map  = array(
            'title' => 'Keywords',
            'info'  => 'ResponseGroup',
            'type'  => 'SearchIndex',
            'id'    => 'ItemId'
        );
        foreach ($map as $old => $new) {
            if (isset($query[$old])) {
                $query[$new] = $query[$old];
                unset($query[$old]);
            }
        }
        // camelize get parameters
        foreach ($query as $key => $val) {  
            if (preg_match('/^[a-z]/', $key)) {
                $query[Inflector::camelize($key)] = $val;
                unset($query[$key]);
            }
        }
        // merge with default options
        // 'Version' => '2008-06-28',
        $query = am(array(
            'Service' => 'AWSECommerceService',
            'AWSAccessKeyId' => $this->config['accessKey'],
            'ResponseGroup' => 'Small',
        ), $query);
        // get the response
        $result = $this->Http->get('http://ecs.amazonaws.com/onca/xml', $query);
        $result = Set::reverse(new Xml($result));
        return $result;
    }
 
    /**
     * close this datasource
     *
     * @access public
     * @return boolean
     * @author primeminister
     */
    function close() {
        return true;
    }
}  
?>