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

发布于 2024-09-19 09:28:17 字数 246 浏览 5 评论 0原文

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

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

因此输入将是
$isan = "0123456789"
输出会是这样的
从亚马逊英国购买:15 英镑从美国亚马逊购买:30 美元

我该怎么做? API 文档很混乱。

I'm listing a specific Amazon product for sale on a site. It's for sale from different Amazon locales, and I would like to list the correct price for these items on my site.

I basically want to input the ASIN/ISBN number and get the price back.

So the input would be
$isan = "0123456789"
And the output would be something like
Buy from Amazon UK: £15 and
Buy from Amazon US: $30

How can I do this? The API documentation is confusing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的笑 2024-09-26 09:28:17
    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;
            }
        }
    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;
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文