Changeset 4920
- Timestamp:
- Jan 12, 2008, 2:44:50 AM (15 years ago)
- Location:
- OpenPNE/trunk/lib/include/Services
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
OpenPNE/trunk/lib/include/Services/Amazon.php
r4854 r4920 69 69 * @author Tatsuya Tsuruoka <ttsuruoka@p4life.jp> 70 70 * @access public 71 * @version Release: 0. 6.071 * @version Release: 0.7.1 72 72 * @uses PEAR 73 73 * @uses HTTP_Request … … 107 107 */ 108 108 var $_baseurl = null; 109 110 /**111 * The proxy parameters to be used by HTTP_Request112 *113 * @access private114 * @var string $_proxy_host115 * @var int $_proxy_port116 * @var string $_proxy_user117 * @var string $_proxy_pass118 */119 var $_proxy_host = null;120 var $_proxy_port = null;121 var $_proxy_user = null;122 var $_proxy_pass = null;123 109 124 110 /** … … 133 119 * @see setLocale 134 120 */ 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') { 136 122 if (!is_null($token)) { 137 123 $this->_token = $token; … … 142 128 } 143 129 144 $this->_locale = $locale;145 130 $this->_baseurl = $baseurl; 131 $this->setLocale($locale); 146 132 } 147 133 … … 250 236 } 251 237 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. 273 242 * 274 243 * @access public … … 278 247 */ 279 248 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]); 281 262 } 282 263 … … 756 737 if (is_array($product->Artists->Artist)) { 757 738 foreach ($product->Artists->Artist as $artist) { 758 $item['artists'][] = $a uthor;739 $item['artists'][] = $artist; 759 740 } 760 741 } else { … … 762 743 } 763 744 } 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; 766 747 $item['imagesmall'] = $product->ImageUrlSmall; 767 748 $item['imagemedium'] = $product->ImageUrlMedium; 768 749 $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; 771 752 772 753 $items[] = $item; … … 800 781 // request for the URL. 801 782 $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 }805 783 $http->addHeader('User-Agent', 'Services_Amazon/' . $this->getApiVersion()); 806 784 $http->sendRequest(); -
OpenPNE/trunk/lib/include/Services/AmazonECS4.php
r2 r4920 94 94 * @author Tatsuya Tsuruoka <ttsuruoka@p4life.jp> 95 95 * @access public 96 * @version Release: 0. 6.096 * @version Release: 0.7.1 97 97 * @uses PEAR 98 98 * @uses HTTP_Request … … 150 150 151 151 /** 152 * The raw result returned from the request 153 * 154 * @access private 155 * @var string 156 */ 157 var $_raw_result = null; 158 159 /** 152 160 * The cache object 153 161 * … … 166 174 */ 167 175 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; 168 208 169 209 /** … … 201 241 function getApiVersion() 202 242 { 203 return '0. 6.0';243 return '0.7.1'; 204 244 } 205 245 … … 348 388 349 389 /** 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 /** 350 407 * Retrieves all error codes and messages 351 408 * … … 406 463 { 407 464 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; 408 476 } 409 477 … … 647 715 $params = $options; 648 716 $params['Operation'] = 'ItemLookup'; 717 if (is_array($item_id)) { 718 $item_id = implode(',', $item_id); 719 } 649 720 $params['ItemId'] = $item_id; 650 721 return $this->_sendRequest($params); … … 800 871 $params = $options; 801 872 $params['Operation'] = 'SimilarityLookup'; 873 if (is_array($item_id)) { 874 $item_id = implode(',', $item_id); 875 } 802 876 $params['ItemId'] = $item_id; 803 877 return $this->_sendRequest($params); … … 957 1031 958 1032 /** 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 { 973 1041 $params['AWSAccessKeyId'] = $this->_keyid; 974 1042 $params['AssociateTag'] = $this->_associd; … … 978 1046 $url .= '&' . $k . '=' . urlencode($v); 979 1047 } 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 { 993 1060 $http = &new HTTP_Request($url); 994 1061 $http->setHttpVer('1.0'); 995 1062 $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 } 997 1071 998 1072 if ($http->getResponseCode() != 200){ 999 1073 return PEAR::raiseError('Amazon returned invalid HTTP response code ' . $http->getResponseCode()); 1000 1074 } 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 { 1003 1087 $xml = &new XML_Unserializer(); 1004 1088 $xml->setOption(XML_UNSERIALIZER_OPTION_ATTRIBUTES_PARSE, true); 1005 1089 $xml->setOption(XML_UNSERIALIZER_OPTION_FORCE_ENUM, 1006 1090 array('Item', 'Review', 'EditorialReview', 1007 'Parameter', 'Author', ' ResponseGroup', 'Error'));1008 $xml->unserialize($r esult, false);1091 'Parameter', 'Author', 'Creator', 'ResponseGroup', 'Error')); 1092 $xml->unserialize($raw_result, false); 1009 1093 $data = $xml->getUnserializedData(); 1010 1094 if (PEAR::isError($data)) { … … 1043 1127 } 1044 1128 } 1045 1046 if ($cache_id) {1047 $this->_cache->save($cache_id, $contents, $this->_cache_expire);1048 }1049 1050 1129 return $contents; 1051 1130 } … … 1057 1136 * @param array $content Values of the content elements 1058 1137 * @return array mixed A PEAR_Error on error, a true on success 1138 * @see _parseRawResult 1059 1139 */ 1060 1140 function _checkContentError($content) … … 1077 1157 return true; 1078 1158 } 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 1079 1210 } 1080 1211 ?>
Note: See TracChangeset
for help on using the changeset viewer.