首页 > 解决方案 > 致命错误:未捕获的 ArgumentCountError:函数 AmazonAPI::construct() 的参数太少,0 已通过

问题描述

我收到此错误

致命错误:未捕获的 ArgumentCountError:函数 AmazonAPI::__construct() 的参数太少,第 151 行的 F:\xampp\htdocs\amazon\index.php 中传递了 0,而 F:\xampp\htdocs\amazon\ 中预期的正是 3 index.php:32 堆栈跟踪:#0 F:\xampp\htdocs\amazon\index.php(151): AmazonAPI->__construct() #1

我该如何解决这个问题,请给我一个解决方案

class AmazonAPI {

    public $amazon_aff_id;
    public $amazon_access_key;
    public $amazon_secret_key;

    public $url_params;
    public $itemID;
    public $xml;

    public $operation;
    public $signature;
    public $response_groups = "Small,Images,OfferSummary";

    public $error_message;
    public $error=0;


    public function __construct($affid, $access, $secret)
    {
        $this->amazon_aff_id = 'mybuyerxpo1-20';
        $this->amazon_access_key = 'AKIAJWDJOLXYKUI0000';
        $this->amazon_secret_key = 'XivGPw/aQfMGXcgi/BqR2F36hO/h1y3rjjfjoook';
    }

    public function build_url()
    {
        $url = "http://webservices.amazon.com/onca/xml?";

        $this->response_groups = str_replace(",", "%2C", $this->response_groups);

        $url_params = "AWSAccessKeyId=" . $this->amazon_access_key;
        $url_params .= "&AssociateTag=" . $this->amazon_aff_id;

        if(!empty($this->itemID)) {
            $url_params .= "&ItemId=" . $this->itemID;
        }

        $url_params .= "&Operation=" . $this->operation;
        $url_params .= "&ResponseGroup=" . $this->response_groups;
        $url_params .= "&Service=AWSECommerceService";
        $url_params .= "&Timestamp=" . rawurlencode(gmdate("2019-10-01\ 16:26:45 UTC\Z"));
        $url_params .= "&Version=2019-10-01";

        $this->url_params = $url_params;

        $url .= $url_params;
        $url .= "&Signature=" . $this->generate_signature();

        return $url;
    }

    public function generate_signature()
    {
        $this->signature = base64_encode(hash_hmac("sha256",
            "GET\nwebservices.amazon.com\n/onca/xml\n" . $this->url_params,
            $this->amazon_secret_key, True));
        $this->signature = str_replace("+", "%2B", $this->signature);
        $this->signature = str_replace("=", "%3D", $this->signature);
        return $this->signature;
    }

    public function item_lookup($id)
    {
        $this->operation = "B07KW8SL6Z";
        $this->itemID = $id;

        $url = $this->build_url();

        $ch = curl_init();  

        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);

        $output = curl_exec($ch);

        curl_close($ch);

        $this->xml = simplexml_load_string($output);
        return $this;
    }

    public function check_for_errors()
    {
        if(isset($this->xml->Error)) {
            $this->error_message = $this->xml->Error->Message;
            $this->error = 1;
        }
        if(isset($this->xml->Items->Request->Errors)) {
            $this->error_message = $this->xml->Items->Request->Errors->Error->Message;
            $this->error = 1;
        }
        return $this->error;
    }

    public function get_item_price($product)
    {
        $price = 0;
        if(isset($product->LowestNewPrice)) {
            $price = $product->LowestNewPrice->Amount;
        } elseif(isset($product->LowestUsedPrice)) {
            $price = $product->LowestUsedPrice->Amount;
        } elseif(isset($product->LowestCollectiblePrice)) {
            $price = $product->LowestCollectiblePrice->Amount;
        } elseif(isset($product->LowestRefurbishedPrice)) {
            $price = $product->LowestRefurbishedPrice->Amount;
        }
        return $price;
    }

    public function get_item_data()
    {
        if($this->check_for_errors()) return null;

        $product = $this->xml->Items->Item;
        $item = new STDclass;
        $item->detailedPageURL = $product->DetailPageURL;
        $item->link = "https://www.amazon.com/gp/product/".$this->itemID."/?tag=" . $this->amazon_aff_id;
        $item->title = $product->ItemAttributes->Title;
        $item->smallImage = $product->SmallImage->URL;
        $item->mediumImage = $product->MediumImage->URL;
        $item->largeImage = $product->LargeImage->URL;

        $item->price = $this->get_item_price($product->OfferSummary);

        return $item;
    }

}

标签: phpamazon-web-services

解决方案


public function __construct($affid, $access, $secret): 你的构造函数需要 3 个参数 : $affid$access$secret你不使用它们。

替换您的构造函数 per __construct(),不带任何参数。


推荐阅读