我如何获得指定亚马逊产品的价格?

时间:2010-09-16 10:57:09

标签: php api amazon

我正在网站上列出特定的亚马逊产品。它是从不同的亚马逊语言区域出售,我想在我的网站上列出这些项目的正确价格。

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

因此输入将是$isan = "0123456789"
输出就像是 Buy from Amazon UK: £15Buy from Amazon US: $30

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

1 个答案:

答案 0 :(得分:0)

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