使用 Amazon API 搜索 ISBN 时出现问题

发布于 2025-01-05 02:27:44 字数 843 浏览 0 评论 0原文

我在使用亚马逊 API 搜索 ISBN 时遇到一些问题。

该代码似乎适用于少数 ISBN,并返回一些结果,但是我通过 ISBN 搜索的大多数书籍(主要是事实/参考书)没有返回任何结果。

为了进行测试,我从亚马逊获取了 ISBN-10 编号。然后我也通过他们自己的搜索来搜索这个 isbn 进行了测试。

这是我们用来获取结果的代码。我不认为有人能发现缺陷吗?

function getBooks($isbn){
    $client = new AmazonECS('AWS_API_KEY', 'AWS_API_SEECRET_KEY', 'co.uk', 'tutorp-21');
    $response  = $client->responseGroup('Small,Images,EditorialReview')->category('Books')->search($isbn);

$books = array();

if($response->Items->TotalResults > 1){
    foreach($response->Items->Item as $item)
        $books[] = parseItem($item);
}else if($response->Items->TotalResults == 1){
    $books[] = parseItem($response->Items->Item);
}

return $books;
}

干杯

编辑:只是为了澄清...我们面临的问题是只有一些 ISBN 号码返回结果。尽管这些书存在于亚马逊中,但通过 API 搜索时它们不会返回任何结果

I'm having a few issues using the amazon API to search for ISBN.

The code seams to work for a FEW isbn's and returns some results however the majority of books (mainly factual/reference books) I search for via ISBN return no results.

To test I am getting the ISBN-10 number from amazon. I have also then tested by searching for this isbn through their own search.

This is the code we use to get the results.. I dont suppose anyone can spot a flaw?

function getBooks($isbn){
    $client = new AmazonECS('AWS_API_KEY', 'AWS_API_SEECRET_KEY', 'co.uk', 'tutorp-21');
    $response  = $client->responseGroup('Small,Images,EditorialReview')->category('Books')->search($isbn);

$books = array();

if($response->Items->TotalResults > 1){
    foreach($response->Items->Item as $item)
        $books[] = parseItem($item);
}else if($response->Items->TotalResults == 1){
    $books[] = parseItem($response->Items->Item);
}

return $books;
}

Cheers

Edit : Just to clarify... The problem we are facing is that only some ISBN numbers return results. Even though these books exist in Amazon they dont seam to return any results when searched through the API

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

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

发布评论

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

评论(2

佞臣 2025-01-12 02:27:44

如果不研究 AmazonECS API,我预计 TotalResults 为 1 时仍会返回包含单个项目的数组;通过 parseItem($response->Items->Item) 在 else 子句中的分配将相应失败(即 books[] 保持为空),因为 $response->Items->Item 仍然是一个数组,无法解析为项目。

因此,您应该删除 else 子句并调整您的条件以测试 0 (或者当然是 >= 1),例如:

// [...]
if($response->Items->TotalResults > 0){
    foreach($response->Items->Item as $item)
        $books[] = parseItem($item);
}
// [...]

更新

显示前 10 个结果 Amazon ECS PHP Library 证实了我的期望,结果循环的实现如下:

//check that there are items in the response
if (isset($response['Items']['Item']) ) {

    //loop through each item
    foreach ($response['Items']['Item'] as $result) {
        // [...]
    }
}

Without looking into the AmazonECS API, I'd expect TotalResults of 1 to return an array containing a single item still; the assignment in your else clause via parseItem($response->Items->Item) will fail accordingly (i.e. books[] remains empty), because $response->Items->Item is still an array and cannot be parsed into an item.

Consequently you should drop the else clause and adjust your condition to test for 0 instead (or >= 1 of course), e.g.:

// [...]
if($response->Items->TotalResults > 0){
    foreach($response->Items->Item as $item)
        $books[] = parseItem($item);
}
// [...]

Update

The Show first 10 results example of the Amazon ECS PHP Library confirms my expectations, the result loop is implemented like so:

//check that there are items in the response
if (isset($response['Items']['Item']) ) {

    //loop through each item
    foreach ($response['Items']['Item'] as $result) {
        // [...]
    }
}
蓝戈者 2025-01-12 02:27:44

问题在于没有社论的书籍。编写的代码工作正常,但需要在没有所有信息的情况下返回书籍的例外情况。

The problems was books that didn't have editorials. The code written works fine but needed exceptions for books being returned without all the information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文