Php 致命错误:未捕获ArgumentCounter错误:函数AmazonAPI::construct()的参数太少,传递了0
我得到了这个错误 致命错误:未捕获ArgumentCounter错误:参数太少,无法 函数AmazonAPI::_construct(),传入0 F:\xampp\htdocs\amazon\index.php位于第151行,在 F:\xampp\htdocs\amazon\index.php:32堆栈跟踪:#0 F:\xampp\htdocs\amazon\index.php(151):AmazonAPI->uuu构造() 我如何解决这个问题请给我一个解决方案Php 致命错误:未捕获ArgumentCounter错误:函数AmazonAPI::construct()的参数太少,传递了0,php,amazon-web-services,Php,Amazon Web Services,我得到了这个错误 致命错误:未捕获ArgumentCounter错误:参数太少,无法 函数AmazonAPI::_construct(),传入0 F:\xampp\htdocs\amazon\index.php位于第151行,在 F:\xampp\htdocs\amazon\index.php:32堆栈跟踪:#0 F:\xampp\htdocs\amazon\index.php(151):AmazonAPI->uuu构造() 我如何解决这个问题请给我一个解决方案 class AmazonAPI
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;
}
}
public function\u构造($affid,$access,$secret)
:您的构造函数需要3个参数:$affid
、$access
和$secret
,但您不使用它们
按照
\u construct()
替换构造函数,不带任何参数。您可以为参数指定默认值,然后在运行时为这些参数提供值,而不是将敏感细节硬编码到类中
同样的想法也可用于具有参数的其他方法,例如,项_lookup
当前有一个$id
属性,但看起来您可能会在那里为$operation
分配另一个参数(除非该参数从未更改)
因此,当调用该类时,您将执行以下操作:
error_reporting( E_ALL );
$affid = 'mybuyerxpo1-20';
$access = 'AKIAJWDJOLXYKUIB537A';
$secret = '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';
$app=new AmazonAPI( $affid, $access, $secret );
如果将默认值分配给
\uu构造
方法中的3个输入参数(即:\uu构造($affid=false,$access=false,$secret=false)
应该可以解决这个问题\u构造($affid,$access,$secret)
这些参数不会在任何地方使用,因此您可以将它们删除$app=new AmazonAPI($affid、$access、$secret);echo$app->get_item_data();我得到以下错误可恢复致命错误:stdClass类的对象无法转换为F:\xampp\htdocs\amazon\index.php中的字符串,第170行的$this->itemID
在上述实现中分配了什么/在哪里?您不能使用echo
输出对象,请使用print\u r
或var\u dump
是的,我在对象中使用了var_dump。我什么都没有得到。你认为这个API是停用的吗?AmazonAPI对象([amazon_aff_id]=>mybuyerxpo1-20[amazon_访问密钥]=>AKIAJF7UYAI77UYKZONA[amazon_密钥]=>ISEZ/SgZ9MV7OzTo5KRnkdhjotvp5tPCWnYL0VnC[url_参数]=>[itemID]=>[xml]>[operation]=>[signature]=>[response]=>=>小图片,提供摘要[错误信息]=>[错误]=>0)否-亚马逊API仍处于活动状态,但我无法测试,因为我没有亚马逊API密钥等(可能给出的详细信息不是真实的!)我和amazon支持人员聊天,他们说API是活动的。但是为什么我不能使用这个脚本来保存数据。你能在你的本地pc上运行这个脚本吗?
error_reporting( E_ALL );
$affid = 'mybuyerxpo1-20';
$access = 'AKIAJWDJOLXYKUIB537A';
$secret = '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';
$app=new AmazonAPI( $affid, $access, $secret );