简体   繁体   English

亚马逊产品广告 API - PHP 致命错误

[英]Amazon Product Advertising API - PHP FATAL ERROR

For a few days I'm trying to connect to Amazon Product Advertising API using PHP.几天来,我一直在尝试使用 PHP 连接到 Amazon Product Advertising API。 I've tried both SDK and none SDK and nothing helps.我已经尝试了 SDK 和无 SDK,但没有任何帮助。

I want to share a none sdk code and maybe get your help on why i'm getting an error.我想分享一个无 sdk 代码,也许可以就我收到错误的原因获得您的帮助。

 <?php /** * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. * A copy of the License is located at * * http://www.apache.org/licenses/LICENSE-2.0 * * or in the "license" file accompanying this file. This file is distributed * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * express or implied. See the License for the specific language governing * permissions and limitations under the License. */ include ("aws.php"); $searchItemRequest = new SearchItemsRequest (); $searchItemRequest->PartnerType = "Associates"; // Put your Partner tag (Store/Tracking id) in place of Partner tag $searchItemRequest->PartnerTag = "***"; $searchItemRequest->Keywords = "Harry"; $searchItemRequest->SearchIndex = "All"; $searchItemRequest->Resources = ["Images.Primary.Small","ItemInfo.Title","Offers.Listings.Price"]; $host = "webservices.amazon.com"; $path = "/paapi5/searchitems"; $payload = json_encode ($searchItemRequest); //Put your Access Key in place of <ACCESS_KEY> and Secret Key in place of <SECRET_KEY> in double quotes $awsv4 = new AwsV4 ("***", "***"); $awsv4->setRegionName("us-east-1"); $awsv4->setServiceName("ProductAdvertisingAPI"); $awsv4->setPath ($path); $awsv4->setPayload ($payload); $awsv4->setRequestMethod ("POST"); $awsv4->addHeader ('content-encoding', 'amz-1.0'); $awsv4->addHeader ('content-type', 'application/json; charset=utf-8'); $awsv4->addHeader ('host', $host); $awsv4->addHeader ('x-amz-target', 'com.amazon.paapi5.v1.ProductAdvertisingAPIv1.SearchItems'); $headers = $awsv4->getHeaders (); $headerString = ""; foreach ( $headers as $key => $value ) { $headerString .= $key . ': ' . $value . "\\r\\n"; } $params = array ( 'http' => array ( 'header' => $headerString, 'method' => 'POST', 'content' => $payload ) ); $stream = stream_context_create ( $params ); $fp = @fopen ( 'https://'.$host.$path, 'rb', false, $stream ); if (! $fp) { throw new Exception ( "Exception Occured" ); } $response = @stream_get_contents ( $fp ); if ($response === false) { throw new Exception ( "Exception Occured" ); } echo $response; class SearchItemsRequest { public $PartnerType; public $PartnerTag; public $Keywords; public $SearchIndex; public $Resources; } ?>

Error is:错误是:

root@difmode:/var/www/html/cms# php index.php PHP Fatal error: Uncaught Exception: Exception Occured in /var/www/html/cms/index.php:56 Stack trace: #0 {main} thrown in /var/www/html/cms/index.php on line 56 root@difmode:/var/www/html/cms# php index.php PHP 致命错误:未捕获异常:异常发生在 /var/www/html/cms/index.php:56 堆栈跟踪:#0 {main} 抛出在第 56 行的 /var/www/html/cms/index.php

If I counted your lines correctly, then the error is thrown by your code:如果我正确计算了您的行数,那么您的代码会抛出错误:

$fp = @fopen ( 'https://'.$host.$path, 'rb', false, $stream );

if (! $fp) {
    throw new Exception ( "Exception Occured" );
}

Basically your $fp is empty.基本上你的 $fp 是空的。 Why, well remove the mutes ( @ ) from your code and you will see the errors.为什么,从您的代码中删除静音( @ ),您将看到错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM