获取产品详细信息

获取产品详细信息

本文介绍了使用 Amazon API 获取产品详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码来输出来自亚马逊的项目列表,但我不确定如何访问特定产品(Summery、评论等).任何帮助将不胜感激.

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

    }







?>

推荐答案

$response->Items->Item 列表包含与您的查询匹配的所有项目/文章.这是对象的列表.这些对象中的每一个都有像 ItemAtributes 这样的属性,这些属性又可以有属性.

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.

查看文档以了解哪些属性是可用的.例如 ItemAttributes->ListPrice->Amount 包含商品的价格.

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;
}

这篇关于使用 Amazon API 获取产品详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 20:35