2009-10-18 189 views
3

我有以下代碼輸出來自亞馬遜的項目列表,但我不確定如何訪問特定產品(包括總結,評論等)。任何幫助,將不勝感激。使用亞馬遜API獲取產品詳細信息

<?php 



    function makeAWSUrl($parameters, $associate_tag, $access_key, $secret_key, $aws_version = '2009-06-01') { 



     $host = 'ecs.amazonaws.com'; 

     $path = '/onca/xml'; 



     $query = array(  

     'Service' => 'AWSECommerceService', 

     'AWSAccessKeyId' => $access_key, 

     'AssociateTag' => $associate_tag, 

     'Timestamp' => gmdate('Y-m-d\TH:i:s\Z'), 

     'Version' => $aws_version, 

     ); 



     // Merge in any options that were passed in 

     if (is_array($parameters)) { 

      $query = array_merge($query, $parameters); 

     } 



     // Do a case-insensitive, natural order sort on the array keys. 

     ksort($query); 



     // create the signable string 

     $temp = array(); 



     foreach ($query as $k => $v) { 

      $temp[] = str_replace('%7E', '~', rawurlencode($k)) . '=' . str_replace('%7E', '~', rawurlencode($v)); 

     } 



     $signable = implode('&', $temp); 



     $stringToSign = "GET\n$host\n$path\n$signable"; 



     // Hash the AWS secret key and generate a signature for the request. 



     $hex_str = hash_hmac('sha256', $stringToSign, $secret_key); 



     $raw = ''; 



     for ($i = 0; $i < strlen($hex_str); $i += 2) { 

      $raw .= chr(hexdec(substr($hex_str, $i, 2))); 

     } 



     $query['Signature'] = base64_encode($raw); 

     ksort($query); 



     $temp = array(); 



     foreach ($query as $k => $v) { 

      $temp[] = rawurlencode($k) . '=' . rawurlencode($v); 

     } 



     $final = implode('&', $temp); 



     return 'http://' . $host . $path . '?' . $final; 

    } 



    $url = makeAWSUrl(array('Keywords' => 'ipod',       

    'Operation' => 'ItemSearch',       

    'SearchIndex' => 'Electronics'), 

    'ResponseGroup' => 'Medium',       

    'someid', 'aaaaaaaaaaaaa', 'aaaaaaaaaaaaaaaaaaaaaaaaaaa'); 





    $response = simplexml_load_file($url); 



    foreach ($response->Items->Item as $item) 

    { 

     $Title [] = $item->ItemAttributes->Title; 

    } 





    foreach($Title as $CurrentTitle) 

    { 

     echo "<h2>".$CurrentTitle."</h2>"; 

    } 







?> 

回答

6

$response->Items->Item列表中包含與您的查詢相匹配的所有項目。這是一個對象列表。每個對象都有屬性,如ItemAtributes,這些對象可以具有屬性。請參閱documentation以查看哪些屬性可用。例如ItemAttributes->ListPrice->Amount包含該物品的價格。

例如要輸出價格和標題爲每個結果的代碼更改

$response = simplexml_load_file($url); 
foreach ($response->Items->Item as $item) { 
    echo "<h2>".$item->ItemAttributes->Title."</h2>"; 
    echo "Price: ".$item->ItemAttributes->ListPrice->Amount; 
} 
+0

謝謝,但我不認爲我是清楚的。我可以獲取特定產品的數據,而不是像上面的代碼那樣獲取許多關鍵字的結果?假設我已經知道asin? – user149109 2009-10-18 21:51:38

+0

非常好,謝謝你的快速回復。 – user149109 2009-10-18 22:48:33

相關問題