使用 Amazon API 搜索 ISBN 时出现问题
我在使用亚马逊 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果不研究
AmazonECS
API,我预计TotalResults
为 1 时仍会返回包含单个项目的数组;通过parseItem($response->Items->Item)
在 else 子句中的分配将相应失败(即books[]
保持为空),因为$response->Items->Item
仍然是一个数组,无法解析为项目。因此,您应该删除 else 子句并调整您的条件以测试 0 (或者当然是
>= 1
),例如:更新
显示前 10 个结果 Amazon ECS PHP Library 证实了我的期望,结果循环的实现如下:
Without looking into the
AmazonECS
API, I'd expectTotalResults
of 1 to return an array containing a single item still; the assignment in your else clause viaparseItem($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.:Update
The Show first 10 results example of the Amazon ECS PHP Library confirms my expectations, the result loop is implemented like so:
问题在于没有社论的书籍。编写的代码工作正常,但需要在没有所有信息的情况下返回书籍的例外情况。
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.