php 我怎样才能得到所有亚马逊类别的产品?

w41d8nur  于 2023-02-18  发布在  PHP
关注(0)|答案(5)|浏览(132)

如何从现有类别中获取所有亚马逊产品?
有了API,我可以浏览10个页面,并获得每个页面10个产品.
该类别有502348件商品,我想把它们都买下来。
下面是我的代码:

Amazon Product Advertising API <?php ?>
$params = array(
        'Operation' => 'ItemSearch',
        'SearchIndex'=>'Electronics',
        //'BrowseNode'=>'281052',
        'ResponseGroup'=>'small',
        //'MerchantId' => 'All',
        //'Condition'=>'New',
        'ItemPage'=>'1471',
vktxenjb

vktxenjb1#

也许你应该尝试在你的查询中添加两个额外的搜索参数-- MinimumPriceMaximumPrice --当然,它们与 keywords 结合在一起。然后,当你的搜索条件超过10页时,你应该修正你的最低/混合价格值。
这就是我如何能够“避免”亚马逊设置的不合理限制。

uurity8g

uurity8g3#

您将看到Amazon强加的新限制。来自产品广告API home page
ItemPage参数将限制为最多10页的ItemSearch结果

nhn9ugyo

nhn9ugyo4#

使用一个循环,从每一页拉所有产品,直到没有更多的页面。

luaexgnf

luaexgnf5#

<?php
namespace MarcL;

use MarcL\CurlHttpRequest;
use MarcL\AmazonUrlBuilder;
use MarcL\Transformers\DataTransformerFactory;

class AmazonAPI
{
    private $urlBuilder = NULL;
    private $dataTransformer = NULL;
    public $item=0;
    public $perRequest=0;
    public function IND_money_format($money)
    {
        $len = strlen($money);
        $m = '';
        $money = strrev($money);
        for($i=0;$i<$len;$i++){
            if(( $i==3 || ($i>3 && ($i-1)%2==0) )&& $i!=$len){
                $m .=',';
            }
            $m .=$money[$i];
        }
        return strrev($m);
    }
    // Valid names that can be used for search
    private $mValidSearchNames = array(
        'All',
        'Apparel',
        'Appliances',
        'Automotive',
        'Baby',
        'Beauty',
        'Blended',
        'Books',
        'Classical',
        'DVD',
        'Electronics',
        'Grocery',
        'HealthPersonalCare',
        'HomeGarden',
        'HomeImprovement',
        'Jewelry',
        'KindleStore',
        'Kitchen',
        'Lighting',
        'Marketplace',
        'MP3Downloads',
        'Music',
        'MusicTracks',
        'MusicalInstruments',
        'OfficeProducts',
        'OutdoorLiving',
        'Outlet',
        'PetSupplies',
        'PCHardware',
        'Shoes',
        'Software',
        'SoftwareVideoGames',
        'SportingGoods',
        'Tools',
        'Toys',
        'VHS',
        'Video',
        'VideoGames',
        'Watches'
    );

    private $mErrors = array();

    public function __construct($urlBuilder, $outputType) {
        $this->urlBuilder = $urlBuilder;
        $this->dataTransformer = DataTransformerFactory::create($outputType);
    }

    public function GetValidSearchNames() {
        return $this->mValidSearchNames;
    }

    /**
     * Search for items
     *
     * @param   keywords            Keywords which we're requesting
     * @param   searchIndex         Name of search index (category) requested. NULL if searching all.
     * @param   sortBy              Category to sort by, only used if searchIndex is not 'All'
     * @param   condition           Condition of item. Valid conditions : Used, Collectible, Refurbished, All
     *
     * @return  mixed               SimpleXML object, array of data or false if failure.
     */
    public function ItemSearch($keywords,$itemPage, $searchIndex = NULL, $sortBy = NULL, $condition = 'All',$minPrice=50000,$maxPrice=55000) {
        ?>
        <table cellpadding="5">
            <thead>
                <tr>
                    <td>Title</td>
                    <td>List Price</td>
                    <td>Offer Price</td>
                    <td>Offer Selling Price</td>
                    <td>Amount Saved</td>
                    <td>Brand Name</td>
                    <td>Size</td>
                    <td>Color</td>
                    <td>Manufacturer</td>
                </tr>
            </thead>
            <tbody> 
            <?php

        $totalPages=0;
        while($maxPrice<=100000)
        {
            $finished=false;
            $itemPage=0;
            while(!$finished)
            {
                $itemPage=$itemPage+1;
                sleep(1);
                $mer="MerchantId";
                $merVal="Amazon";
                $params = array(
                    'Operation' => 'ItemSearch',
                    'ResponseGroup' => 'Small,ItemAttributes,Offers,OfferSummary,BrowseNodes',
                    'Keywords' => $keywords,
                    'Condition' => $condition,
                    'ItemPage' => $itemPage,
                    'ListPrice' => $itemPage,
                    'MinimumPrice' => $minPrice,
                    'MaximumPrice' => $maxPrice,
                    'SearchIndex' => empty($searchIndex) ? 'All' : $searchIndex,
                    'Sort' => $sortBy && ($searchIndex != 'All') ? $sortBy : NULL
                );
                $totalPages=$this->FetchItems($params,$itemPage,$maxPrice,false);
                if(($itemPage)==1)
                {
                    $finished=true;
                    $itemPage=0;
                }
            }
            $minPrice=$maxPrice;
            $maxPrice=$maxPrice+5000;
        }
        //echo "<br/>total Records : ".$this->item;
        ?>
            </tbody>
        </table>
        <br/><br/>

        <?php
        $style="";
                for($looper=1;$looper<=$totalPages;$looper++)
                {
                    if($looper>($itemPage-3) && $looper<($itemPage+3))
                    {
                        if($looper==$itemPage)
                        {
                            $style="style='color:red;'";
                            echo "<a href='examples.php?itemPage=".$looper."' ".$style.">".$looper."</a>&nbsp;&nbsp;&nbsp;";
                        }
                        else 
                        {
                            echo "<a href='examples.php?itemPage=".$looper."'>".$looper."</a>&nbsp;&nbsp;&nbsp;";
                        }
                    }else if($looper>($totalPages-3))
                    {

                        echo "<a href='examples.php?itemPage=".$looper."'>".$looper."</a>&nbsp;&nbsp;&nbsp;";
                    }else if($looper>(($totalPages/2)-3) && $looper<(($totalPages/2)+3))
                    {

                        echo "<a href='examples.php?itemPage=".$looper."'>".$looper."</a>&nbsp;&nbsp;&nbsp;";
                    }
                }
        die();
        //return $this->MakeAndParseRequest($params,$itemPage);
    }

    /**
     * Lookup items from ASINs
     *
     * @param   asinList            Either a single ASIN or an array of ASINs
     * @param   onlyFromAmazon      True if only requesting items from Amazon and not 3rd party vendors
     *
     * @return  mixed               SimpleXML object, array of data or false if failure.
     */
    public function ItemLookup($asinList,$itemPage, $onlyFromAmazon = false) {
        $asinList="B01D0XDW1C";
        if (is_array($asinList)) {
            $asinList = implode(',', $asinList);
        }

        $params = array(
            'Operation' => 'ItemLookup',
            'ResponseGroup' => 'ItemAttributes,Offers,Images',
            'ReviewSort' => '-OverallRating',
            'ItemId' => $asinList,
            'MerchantId' => ($onlyFromAmazon == true) ? 'Amazon' : 'All'
        );

        return $this->MakeAndParseRequest($params,$itemPage,true);
    }

    public function GetErrors() {
        return $this->mErrors;
    }

    private function AddError($error) {
        array_push($this->mErrors, $error);
    }
    public function FetchItems($params,$itemPage,$maxPrice,$lookup=false)
    {
        $signedUrl = $this->urlBuilder->generate($params);
            if($lookup)
            {
                try 
                {
                    $request = new CurlHttpRequest();
                    $response = $request->execute($signedUrl);
                    $fileContents = str_replace(array("\n", "\r", "\t"), '', $response);
                    $fileContents = trim(str_replace('"', "'", $fileContents));
                    $simpleXml = simplexml_load_string($fileContents);
                    $json = json_encode($simpleXml);
                    $decodedJson=json_decode($json,true);

                        //print_r($decodedJson);
                    print_r($decodedJson);
                    die();
                    $parsedXml = simplexml_load_string($response);

                    if ($parsedXml === false) {
                        return false;
                    }

                    return $this->dataTransformer->execute($parsedXml);
                } catch(\Exception $error) {
                $this->AddError("Error downloading data : $signedUrl : " . $error->getMessage());
                return false;
                }
            }
            else
            {
                try 
                {
                    $request = new CurlHttpRequest();
                    $response = $request->execute($signedUrl);
                    $fileContents = str_replace(array("\n", "\r", "\t"), '', $response);
                    $fileContents = trim(str_replace('"', "'", $fileContents));
                    $simpleXml = simplexml_load_string($fileContents);
                    $json = json_encode($simpleXml);
                    $decodedJson=json_decode($json,true);
                    print_r($decodedJson);
                    die();
                    if(isset($decodedJson['Items']))
                    {
                        $this->perRequest=0;
                        foreach($decodedJson['Items']['Item'] as $itm)
                        {
                            if(isset($itm['ItemAttributes']['ListPrice']['FormattedPrice']))
                            {
                                $this->item=$this->item+1;
                                $this->perRequest=$this->perRequest+1;

                                ?>
                                <tr>
                                    <td>
                                        <?php
                                            if(isset($itm['ItemAttributes']['Title']))
                                                echo $itm['ItemAttributes']['Title'];
                                            else 
                                                echo "N/A";
                                        ?>
                                    </td>
                                    <td>
                                        <?php
                                            if(isset($itm['ItemAttributes']['ListPrice']['FormattedPrice']))
                                                echo $itm['ItemAttributes']['ListPrice']['FormattedPrice'];
                                            else 
                                                echo "N/A";
                                        ?>                          
                                    </td>
                                    <?php
                                        $savedAmount=0;
                                        if(isset($itm['Offers']['Offer']['OfferListing']['Price']['FormattedPrice']))
                                        {
                                            ?>
                                                <td><?php echo $itm['Offers']['Offer']['OfferListing']['Price']['FormattedPrice']; ?></td>
                                            <?php
                                            if(isset($itm['Offers']['Offer']['OfferListing']['SalePrice']['FormattedPrice']))
                                            {
                                                $total=(int)($itm['ItemAttributes']['ListPrice']['Amount']);
                                                $offer=(int)($itm['Offers']['Offer']['OfferListing']['SalePrice']['Amount']);
                                                $savedAmount=$total-$offer;
                                                $savedAmount=$savedAmount/100;
                                                $savedAmount=$this->IND_money_format($savedAmount);
                                                $savedAmount="INR ".$savedAmount.".00";
                                                ?>
                                                    <td><?php echo $itm['Offers']['Offer']['OfferListing']['SalePrice']['FormattedPrice']; ?></td>
                                                    <td><?php echo $savedAmount; ?></td>
                                                <?php
                                            }
                                            else
                                            {
                                                $total=(int)($itm['ItemAttributes']['ListPrice']['Amount']);
                                                $offer=(int)($itm['Offers']['Offer']['OfferListing']['Price']['Amount']);
                                                $savedAmount=$total-$offer;
                                                $savedAmount=$savedAmount/100;
                                                $savedAmount=$this->IND_money_format($savedAmount);
                                                $savedAmount="INR ".$savedAmount.".00";
                                                ?>
                                                <td><?php echo $itm['Offers']['Offer']['OfferListing']['Price']['FormattedPrice']; ?></td>
                                                <td><?php echo $savedAmount; ?></td>
                                                <?php
                                            }
                                        }
                                        else if(isset($itm['OfferSummary']['LowestNewPrice']['FormattedPrice']))
                                        {
                                            $total=(int)($itm['ListPrice']['Amount']);
                                            $offer=(int)($itm['Offers']['Offer']['OfferListing']['SalePrice']['Amount']);
                                            $savedAmount=$total-$offer;
                                            $savedAmount=$savedAmount/100;
                                            $savedAmount=$this->IND_money_format($savedAmount);
                                                $savedAmount="INR ".$savedAmount.".00";
                                            ?>
                                                <td><?php echo $itm['OfferSummary']['LowestNewPrice']['FormattedPrice']; ?></td>
                                                <td><?php echo $itm['OfferSummary']['LowestNewPrice']['FormattedPrice']; ?></td>
                                                <td><?php echo $savedAmount; ?></td>
                                            <?php
                                        }
                                        else
                                        {
                                            ?>
                                                <td>N/A</td>
                                                <td>N/A</td>
                                                <td>N/A</td>
                                            <?php
                                        }
                                    ?>  
                                    <td>
                                        <?php
                                            if(isset($itm['ItemAttributes']['Brand']))
                                                echo $itm['ItemAttributes']['Brand'];
                                            else 
                                                echo "N/A";
                                        ?>                      
                                    </td>

                                    <td>
                                        <?php
                                            if(isset($itm['ItemAttributes']['Size']))
                                                echo $itm['ItemAttributes']['Size'];
                                            else 
                                                echo "N/A";
                                        ?>                      
                                    </td>
                                    <td>
                                        <?php
                                            if(isset($itm['ItemAttributes']['Color']))
                                                echo $itm['ItemAttributes']['Color'];
                                            else 
                                                echo "N/A";
                                        ?>              
                                    </td>
                                    <td>
                                        <?php
                                            if(isset($itm['ItemAttributes']['Manufacturer']))
                                                echo $itm['ItemAttributes']['Manufacturer'];
                                            else 
                                                echo "N/A";
                                        ?>                              
                                    </td>
                                </tr>
                                <?php

                            }
                        }
                        //return 
                        //echo $maxPrice." : ".$decodedJson['Items']['TotalPages']."<br/>";
                    }
                    //echo "PerRequest : ".$this->perRequest."<br/>";
                    //die();
                    //$parsedXml = simplexml_load_string($response);

                    //if ($parsedXml === false) {
                    //  return false;
                    //}

                    //return $this->dataTransformer->execute($parsedXml);
                } catch(\Exception $error) {
                $this->AddError("Error downloading data : $signedUrl : " . $error->getMessage());
                return false;
                }
            }
    }
    private function MakeAndParseRequest($params,$itemPage,$lookup=false) 
    {
        $this->item=0;

                /*$style="";
                for($looper=1;$looper<=$totalPages;$looper++)
                {
                    if($looper>($itemPage-3) && $looper<($itemPage+3))
                    {
                        if($looper==$itemPage)
                        {
                            $style="style='color:red;'";
                            echo "<a href='examples.php?itemPage=".$looper."' ".$style.">".$looper."</a>&nbsp;&nbsp;&nbsp;";
                        }
                        else 
                        {
                            echo "<a href='examples.php?itemPage=".$looper."'>".$looper."</a>&nbsp;&nbsp;&nbsp;";
                        }
                    }else if($looper>($totalPages-3))
                    {

                        echo "<a href='examples.php?itemPage=".$looper."'>".$looper."</a>&nbsp;&nbsp;&nbsp;";
                    }else if($looper>(($totalPages/2)-3) && $looper<(($totalPages/2)+3))
                    {

                        echo "<a href='examples.php?itemPage=".$looper."'>".$looper."</a>&nbsp;&nbsp;&nbsp;";
                    }
                }
                */
    }
}
?>

相关问题