ここの情報は古いです。ご理解頂いた上でお取り扱いください。

Changeset 4920


Ignore:
Timestamp:
Jan 12, 2008, 2:44:50 AM (15 years ago)
Author:
ebihara
Message:

#1747:PEAR::Services_Amazonのバージョンを0.6.0から0.7.1に上げた

Location:
OpenPNE/trunk/lib/include/Services
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • OpenPNE/trunk/lib/include/Services/Amazon.php

    r4854 r4920  
    6969* @author  Tatsuya Tsuruoka <ttsuruoka@p4life.jp>
    7070* @access  public
    71 * @version Release: 0.6.0
     71* @version Release: 0.7.1
    7272* @uses    PEAR
    7373* @uses    HTTP_Request
     
    107107    */
    108108    var $_baseurl = null;
    109  
    110    /**
    111     * The proxy parameters to be used by HTTP_Request
    112     *
    113     * @access private
    114     * @var    string $_proxy_host
    115     * @var    int    $_proxy_port
    116     * @var    string $_proxy_user
    117     * @var    string $_proxy_pass
    118     */
    119     var $_proxy_host = null;
    120     var $_proxy_port = null;
    121     var $_proxy_user = null;
    122     var $_proxy_pass = null;
    123109
    124110    /**
     
    133119    * @see    setLocale
    134120    */
    135     function Services_Amazon($token = null, $affid = null, $locale = 'us', $baseurl = 'http://xml.amazon.com/onca/xml2') {
     121    function Services_Amazon($token = null, $affid = null, $locale = 'us', $baseurl = 'http://webservices.amazon.com/onca/xml2') {
    136122        if (!is_null($token)) {
    137123            $this->_token = $token;
     
    142128        }
    143129       
    144         $this->_locale  = $locale;
    145130        $this->_baseurl = $baseurl;
     131        $this->setLocale($locale);
    146132    }
    147133
     
    250236    }
    251237   
    252    /**
    253     * Sets a proxy to be used by HTTP_Request
    254     *
    255     * @param string     Proxy host
    256     * @param int        Proxy port
    257     * @param string     Proxy username
    258     * @param string     Proxy password
    259     * @access public
    260     */
    261     function setProxy($host, $port = 8080, $user = null, $pass = null)
    262     {
    263         $this->_proxy_host = $host;
    264         $this->_proxy_port = $port;
    265         $this->_proxy_user = $user;
    266         $this->_proxy_pass = $pass;
    267     }
    268    
    269     /**
    270     * Sets the locale passed when making a query to Amazon.com.
    271     *
    272     * Currently only us, uk, and de are supported by Amazon.
     238    /**
     239    * Sets the locale passed when making a query to Amazon.
     240    *
     241    * Currently only us, uk, de, jp, fr and ca are supported by Amazon.
    273242    *
    274243    * @access public
     
    278247    */   
    279248    function setLocale($locale) {
    280         $this->_locale = $locale;
     249        $urls = array(
     250            'us' => 'http://webservices.amazon.com/onca/xml2',
     251            'uk' => 'http://webservices.amazon.co.uk/onca/xml2',
     252            'de' => 'http://webservices.amazon.de/onca/xml2',
     253            'jp' => 'http://webservices.amazon.co.jp/onca/xml2',
     254            'fr' => 'http://webservices.amazon.fr/onca/xml2',
     255            'ca' => 'http://webservices.amazon.ca/onca/xml2',
     256        );
     257        $this->_locale = strtolower($locale);
     258        if (empty($urls[$locale])) {
     259            return;
     260        }
     261        $this->setBaseUrl($urls[$locale]);
    281262    }
    282263   
     
    756737                if (is_array($product->Artists->Artist)) {
    757738                    foreach ($product->Artists->Artist as $artist) {
    758                         $item['artists'][] = $author;
     739                        $item['artists'][] = $artist;
    759740                    }
    760741                } else {
     
    762743                }
    763744            }
    764             $item['release']      = $product->ReleaseDate;
    765             $item['manufacturer'] = $product->Manufacturer;
     745            $item['release']      = isset($product->ReleaseDate) ? $product->ReleaseDate : null;
     746            $item['manufacturer'] = isset($product->Manufacturer) ? $product->Manufacturer : null;
    766747            $item['imagesmall']   = $product->ImageUrlSmall;
    767748            $item['imagemedium']  = $product->ImageUrlMedium;
    768749            $item['imagelarge']   = $product->ImageUrlLarge;
    769             $item['listprice']    = $product->ListPrice;
    770             $item['ourprice']     = $product->ListPrice;
     750            $item['listprice']    = isset($product->ListPrice) ? $product->ListPrice : null;
     751            $item['ourprice']     = isset($product->ListPrice) ? $product->ListPrice : null;
    771752
    772753            $items[] = $item;
     
    800781        // request for the URL.
    801782        $http = &new HTTP_Request($url);
    802         if ($this->_proxy_host) {
    803             $http->setProxy($this->_proxy_host, $this->_proxy_port, $this->_proxy_user, $this->_proxy_pass);
    804         }
    805783        $http->addHeader('User-Agent', 'Services_Amazon/' . $this->getApiVersion());
    806784        $http->sendRequest();
  • OpenPNE/trunk/lib/include/Services/AmazonECS4.php

    r2 r4920  
    9494* @author  Tatsuya Tsuruoka <ttsuruoka@p4life.jp>
    9595* @access  public
    96 * @version Release: 0.6.0
     96* @version Release: 0.7.1
    9797* @uses    PEAR
    9898* @uses    HTTP_Request
     
    150150
    151151    /**
     152    * The raw result returned from the request
     153    *
     154    * @access private
     155    * @var    string
     156    */
     157    var $_raw_result = null;
     158
     159    /**
    152160    * The cache object
    153161    *
     
    166174    */
    167175    var $_cache_expire = 3600;
     176
     177    /**
     178    * Proxy server
     179    *
     180    * @access private
     181    * @var    string
     182    */
     183    var $_proxy_host = null;
     184
     185    /**
     186    * Proxy port
     187    *
     188    * @access private
     189    * @var    integer
     190    */
     191    var $_proxy_port = null;
     192
     193    /**
     194    * Proxy username
     195    *
     196    * @access private
     197    * @var    string
     198    */
     199    var $_proxy_user = null;
     200
     201    /**
     202    * Proxy password
     203    *
     204    * @access private
     205    * @var    string
     206    */
     207    var $_proxy_pass = null;
    168208
    169209    /**
     
    201241    function getApiVersion()
    202242    {
    203         return '0.6.0';
     243        return '0.7.1';
    204244    }
    205245
     
    348388
    349389    /**
     390    * Sets a proxy
     391    *
     392    * @access public
     393    * @param string $host Proxy host
     394    * @param int $port Proxy port
     395    * @param string $user Proxy username
     396    * @param string $pass Proxy password
     397    */
     398    function setProxy($host, $port = 8080, $user = null, $pass = null)
     399    {
     400        $this->_proxy_host = $host;
     401        $this->_proxy_port = $port;
     402        $this->_proxy_user = $user;
     403        $this->_proxy_pass = $pass;
     404    }
     405
     406    /**
    350407    * Retrieves all error codes and messages
    351408    *
     
    406463    {
    407464        return $this->_lasturl;
     465    }
     466
     467    /**
     468     * Retrieves the raw result
     469     *
     470     * @access public
     471     * @return string The raw result
     472     */
     473    function getRawResult()
     474    {
     475        return $this->_raw_result;
    408476    }
    409477
     
    647715        $params = $options;
    648716        $params['Operation'] = 'ItemLookup';
     717        if (is_array($item_id)) {
     718            $item_id = implode(',', $item_id);
     719        }
    649720        $params['ItemId'] = $item_id;
    650721        return $this->_sendRequest($params);
     
    800871        $params = $options;
    801872        $params['Operation'] = 'SimilarityLookup';
     873        if (is_array($item_id)) {
     874            $item_id = implode(',', $item_id);
     875        }
    802876        $params['ItemId'] = $item_id;
    803877        return $this->_sendRequest($params);
     
    9571031
    9581032    /**
    959     * Sends the request to Amazon
    960     *
    961     * @access private
    962     * @param  array $params The array of request parameters
    963     * @return array The array of information returned by the query
    964     */
    965     function _sendRequest($params)
    966     {
    967         $this->_errors = array();
    968 
    969         if (is_null($this->_keyid)) {
    970             return PEAR::raiseError('Access Key ID have not been set');
    971         }
    972 
     1033    * Builds a URL
     1034    *
     1035    * @access private
     1036    * @param array $params
     1037    * @return string URL
     1038    */
     1039    function _buildUrl($params)
     1040    {
    9731041        $params['AWSAccessKeyId'] = $this->_keyid;
    9741042        $params['AssociateTag'] = $this->_associd;
     
    9781046            $url .= '&' . $k . '=' . urlencode($v);
    9791047        }
    980         $this->_lasturl = $url;
    981 
    982         // Return cached data if available
    983         $cache_id = false;
    984         if (isset($this->_cache) && !$this->_ignoreCache($params['Operation'])) {
    985             $cache_id = $this->_generateCacheId($params);
    986             $cache = $this->_cache->get($cache_id);
    987             if (!is_null($cache)) {
    988                 $this->_processing_time = 0;
    989                 return $cache;
    990             }
    991         }
    992 
     1048        return $url;
     1049    }
     1050
     1051    /**
     1052    * Sends a request
     1053    *
     1054    * @access private
     1055    * @param string $url
     1056    * @return string The response
     1057    */
     1058    function _sendHttpRequest($url)
     1059    {
    9931060        $http = &new HTTP_Request($url);
    9941061        $http->setHttpVer('1.0');
    9951062        $http->addHeader('User-Agent', 'Services_AmazonECS4/' . $this->getApiVersion());
    996         $http->sendRequest();
     1063        if ($this->_proxy_host) {
     1064            $http->setProxy($this->_proxy_host, $this->_proxy_port, $this->_proxy_user, $this->_proxy_pass);
     1065        }
     1066
     1067        $result = $http->sendRequest();
     1068        if (PEAR::isError($result)) {
     1069            return PEAR::raiseError('HTTP_Request::sendRequest failed: ' . $result->message);
     1070        }
    9971071
    9981072        if ($http->getResponseCode() != 200){
    9991073            return PEAR::raiseError('Amazon returned invalid HTTP response code ' . $http->getResponseCode());
    10001074        }
    1001         $result = $http->getResponseBody();
    1002 
     1075        return $http->getResponseBody();
     1076    }
     1077
     1078    /**
     1079    * Parses raw XML result
     1080    *
     1081    * @access private
     1082    * @param string $raw_result
     1083    * @return string The contents
     1084    */
     1085    function _parseRawResult($raw_result)
     1086    {
    10031087        $xml = &new XML_Unserializer();
    10041088        $xml->setOption(XML_UNSERIALIZER_OPTION_ATTRIBUTES_PARSE, true);
    10051089        $xml->setOption(XML_UNSERIALIZER_OPTION_FORCE_ENUM,
    10061090                        array('Item', 'Review', 'EditorialReview',
    1007                               'Parameter', 'Author', 'ResponseGroup', 'Error'));
    1008         $xml->unserialize($result, false);
     1091                              'Parameter', 'Author', 'Creator', 'ResponseGroup', 'Error'));
     1092        $xml->unserialize($raw_result, false);
    10091093        $data = $xml->getUnserializedData();
    10101094        if (PEAR::isError($data)) {
     
    10431127            }
    10441128        }
    1045 
    1046         if ($cache_id) {
    1047             $this->_cache->save($cache_id, $contents, $this->_cache_expire);
    1048         }
    1049 
    10501129        return $contents;
    10511130    }
     
    10571136    * @param  array $content Values of the content elements
    10581137    * @return array mixed A PEAR_Error on error, a true on success
     1138    * @see    _parseRawResult
    10591139    */
    10601140    function _checkContentError($content)
     
    10771157        return true;
    10781158    }
     1159
     1160    /**
     1161    * Sends the request to Amazon
     1162    *
     1163    * @access private
     1164    * @param  array $params The array of request parameters
     1165    * @return array The array of information returned by the query
     1166    */
     1167    function _sendRequest($params)
     1168    {
     1169        $this->_errors = array();
     1170
     1171        if (is_null($this->_keyid)) {
     1172            return PEAR::raiseError('Access Key ID have not been set');
     1173        }
     1174
     1175        $url = $this->_buildUrl($params);
     1176        $this->_lasturl = $url;
     1177        if (PEAR::isError($url)) {
     1178            return $url;
     1179        }
     1180
     1181        // Return cached data if available
     1182        $cache_id = false;
     1183        if (isset($this->_cache) && !$this->_ignoreCache($params['Operation'])) {
     1184            $cache_id = $this->_generateCacheId($params);
     1185            $cache = $this->_cache->get($cache_id);
     1186            if (!is_null($cache)) {
     1187                $this->_processing_time = 0;
     1188                return $cache;
     1189            }
     1190        }
     1191
     1192        $result = $this->_sendHttpRequest($url);
     1193        $this->_raw_result = $result;
     1194        if (PEAR::isError($result)) {
     1195            return $result;
     1196        }
     1197
     1198        $contents = $this->_parseRawResult($result);
     1199        if (PEAR::isError($contents)) {
     1200            return $contents;
     1201        }
     1202
     1203        if ($cache_id) {
     1204            $this->_cache->save($cache_id, $contents, $this->_cache_expire);
     1205        }
     1206
     1207        return $contents;
     1208    }
     1209
    10791210}
    10801211?>
Note: See TracChangeset for help on using the changeset viewer.