Getting product details using Amazon API

时光总嘲笑我的痴心妄想 提交于 2019-12-06 07:45:35

问题


I've got the following code to output a list of items from amazon, but I not sure how to access specific products (with Summery, reviews, etc). Any help would be appreciated.

<?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>";

    }







?>

回答1:


The $response->Items->Item list contains all the items/articles which matches your query. This is a list of objects. Each of this objects got properties like ItemAtributes which agains can have properties.

Have a look at the documentation to see which attributes are avalable. For example ItemAttributes->ListPrice->Amount contains the price of the item.

So for example to output price and title for each result change the code to

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


来源:https://stackoverflow.com/questions/1586086/getting-product-details-using-amazon-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!