【问题标题】:How do I get the price of a specified Amazon product?如何获取指定亚马逊产品的价格?
【发布时间】:2010-09-16 10:57:09
【问题描述】:

我正在一个网站上列出要出售的特定亚马逊产品。它在不同的亚马逊地区出售,我想在我的网站上列出这些商品的正确价格。

我基本上是想输入 ASIN/ISBN 号并取回价格。

所以输入将是
$isan = "0123456789"
输出将类似于
Buy from Amazon UK: £15Buy from Amazon US: $30

我该怎么做? API 文档令人困惑。

【问题讨论】:

  • 您不需要在标题中添加“[PHP]”,这就是标签的用途,而且它们被广泛使用。
  • 您需要提供更多关于您已经完成的工作以及您正在使用的 Amazon API 的信息。目前,您要求我们为您编写解决方案,而不是提出具体的问题/问题。

标签: php api amazon


【解决方案1】:
    public function get_amazon_price() {
       $response = $this->getAmazonPrice("in", "B00SZQ4A70");
       var_dump($response);
    }

    function getAmazonPrice($region, $asin) {

            $xml = $this->aws_signed_request($region, array(
                "Operation" => "ItemLookup",
                "ItemId" => $asin,
                "IncludeReviewsSummary" => False,
                "ResponseGroup" => "Medium",
            ));

            $item = $xml->Items->Item;
            //var_dump($item);
            $title = htmlentities((string) $item->ItemAttributes->Title);
            $url = htmlentities((string) $item->DetailPageURL);
            $image = htmlentities((string) $item->MediumImage->URL);
            $price = htmlentities((string) $item->OfferSummary->LowestNewPrice->Amount);
            $discount_price = htmlentities((string) $item->OfferSummary->LowestNewPrice->FormattedPrice);
            $code = htmlentities((string) $item->OfferSummary->LowestNewPrice->CurrencyCode);
            $qty = htmlentities((string) $item->OfferSummary->TotalNew);

            $mrp = htmlentities((string) $item->ItemAttributes->ListPrice->FormattedPrice);



            $response = '';
            if ($qty !== "0") {
                if ($mrp == '') {
                    //echo "MRP = " . $discount_price . '<br/>';
                    //echo "discount_price = 0<br/><hr/>";
                    $response = array(
                        "mrp" => str_replace('INR ', '', str_replace(',', '', $discount_price))
                    );
                } else {
                    // echo "discount_price = " . $discount_price . '<br/>';
                    //echo "MRP = " . $mrp . '<br/><hr/>';
                    $response = array(
                        "discount_price" => str_replace('INR ', '', str_replace(',', '', $discount_price)),
                        "mrp" => str_replace('INR ', '', str_replace(',', '', $mrp))
                    );
                }
            }

            return $response;
            }

        function getPage($url) {

            $curl = curl_init($url);
            curl_setopt($curl, CURLOPT_FAILONERROR, true);
            curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            $html = curl_exec($curl);
            curl_close($curl);
            return $html;
        }

        function aws_signed_request($region, $params) {

            $public_key = "xxx";
            $private_key = "xxx";

            $method = "GET";
            $host = "ecs.amazonaws." . $region;
            $host = "webservices.amazon." . $region;
            $uri = "/onca/xml";

            $params["Service"] = "AWSECommerceService";
            $params["AssociateTag"] = ""; // Put your Affiliate Code here
            $params["AWSAccessKeyId"] = $public_key;
            $params["Timestamp"] = gmdate("Y-m-d\TH:i:s\Z");
            $params["Version"] = "2011-08-01";

            ksort($params);

            $canonicalized_query = array();
            foreach ($params as $param => $value) {
                $param = str_replace("%7E", "~", rawurlencode($param));
                $value = str_replace("%7E", "~", rawurlencode($value));
                $canonicalized_query[] = $param . "=" . $value;
            }

            $canonicalized_query = implode("&", $canonicalized_query);

            $string_to_sign = $method . "\n" . $host . "\n" . $uri . "\n" . $canonicalized_query;
            $signature = base64_encode(hash_hmac("sha256", $string_to_sign, $private_key, True));
            $signature = str_replace("%7E", "~", rawurlencode($signature));

            $request = "http://" . $host . $uri . "?" . $canonicalized_query . "&Signature=" . $signature;
            $response = $this->getPage($request);

            //var_dump($response);

            $pxml = @simplexml_load_string($response);
            if ($pxml === False) {
                return False; // no xml
            } else {
                return $pxml;
            }
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多