The single
public method, which will be called from the upper tiers, is GetProducts(), whereas the others are private
methods for internal use that support the functionality of GetProducts().
// Class for accessing A2S
class Amazon
{
public function Amazon()
{
}
// Retrieves Amazon products for sending to presentation tier
public function GetProducts()
{
// Use SOAP to get data
if (AMAZON_METHOD == 'SOAP')
$result = $this->_GetDataWithSoap();
// Use REST to get data
else
$result = $this->_GetDataWithRest();
CHAPTER 22 ?– USING AMAZON.COM WEB SERVICES 677
// Initializes Array object
$results = array ();
// Format results
$results = $this->_DataFormat($result);
// Returns results
return $results;
}
// Call A2S using REST
private function _GetDataWithRest()
{
$params = array ('Operation' => 'ItemSearch',
'SubscriptionId' => AMAZON_ACCESS_KEY_ID,
'Keywords' => AMAZON_SEARCH_KEYWORDS,
'SearchIndex' => AMAZON_SEARCH_NODE,
'ResponseGroup' => AMAZON_RESPONSE_GROUPS,
'Sort' => 'salesrank');
$query_string = '&';
foreach ($params as $key => $value)
$query_string .= $key . '=' . urlencode($value) . '&';
$amazon_url = AMAZON_REST_BASE_URL . $query_string;
// Get the XML response using REST
$amazon_xml = file_get_contents($amazon_url);
// Unserialize the XML and return
return simplexml_load_string($amazon_xml);
}
// Call A2S using SOAP
private function _GetDataWithSoap()
{
try
{
$client = new SoapClient(AMAZON_WSDL);
/* Set up an array containing input parameters to be
passed to the remote procedure */
$request = array ('SubscriptionId' => AMAZON_ACCESS_KEY_ID,
'Request' => array ('Operation' =>
'ItemSearchRequest',
'Keywords' =>
AMAZON_SEARCH_KEYWORDS,
'SearchIndex' =>
CHAPTER 22 ?– USING AMAZON.
Pages:
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792