如何使用Zend服务Amazon? [英] How to use Zend Service Amazon?

查看:185
本文介绍了如何使用Zend服务Amazon?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在做很多的挖掘,结果是一个巨大的头痛,没有成功。我试图使用Zend亚马逊服务(在我的 Codeigniter 框架内)使用其ISBN获取有关图书的信息。我最初尝试它与Zend 1.12但我不断收到错误关于缺少参数AssociateTag。我现在尝试与Zend 2.0,但仍然有问题。

I've been doing a lot of digging around and have ended up with a huge headache and no success. I am trying to use the Zend Amazon Service (within my Codeigniter framework) to fetch information about a book using its ISBN. I initially tried it with Zend 1.12 but I kept getting an error about missing parameter AssociateTag. I am now trying with Zend 2.0 but still getting problems.

这是我在我的控制器中使用的代码:

This is the code I am using within my controller:

    set_include_path(get_include_path() . PATH_SEPARATOR . 'site/libraries');
    require_once 'Zend2/Loader/StandardAutoloader.php';
    $autoloader = Zend_Loader_Autoloader::getInstance();
    $amazon = new Zend_Service_Amazon('[apikey]', 'US', '[secretkey]');
    $item = $amazon->itemLookup('B0000A432X');

我得到以下错误:


致命错误:找不到类'Zend_Loader_Autoloader'。

Fatal error: Class 'Zend_Loader_Autoloader' not found.

我的问题是:


  • 如何加载正确的自动加载器?

  • 在参数中包含关联标签的位置?在过时的zend用户指南中没有提到它

  • Zend 2.0甚至似乎没有亚马逊服务文件,甚至没有/ service文件夹。这是什么意思?

推荐答案

与zendservice-amazon不工作。请求必须包含您的应用ID,密钥和关联标签,演示脚本默认不做。我花了一段时间想出这一点,没有这些,所有的查询都抛出一个异常说HTTP响应状态是400.不幸的是,异常没有响应体,说什么参数丢失。

Note: The demo included with zendservice-amazon doesn't work as is. Requests must include your App ID, Secret Key, and Associate Tag which the demo script doesn't do by default. It took me a while to figure this out, without these, all queries throw an exception saying the HTTP response status was 400. Unfortunately the exception doesn't have the response body which says what parameters were missing.

这里是一些代码,将让你开始使用ZF2和 ZendService\Amazon

Here is some code that will get you started with ZF2 and ZendService\Amazon.

首先,让我概述一下我将把文件放在这个例子中的目录结构:

To start, let me outline the directory structure of where I will be putting files for this example:

testing
|-Zend
|---Crypt
|---Escaper
|---Http 
|---I18n
|---Loader
|----+AutoloaderFactory.php
|----+... more files
|----+StandardAutoloader.php
|-----Exception
|---Stdlib
|---Uri
|---Validator
|-ZendRest
|-ZendService
|---Amazon
|-----Authentication
|-------Exception
|-----Ec2
|-------Exception
|-----Exception
|-----S3
|-------Exception
|-----SimpleDb
|-------Exception
|-----Sqs
|-------Exception
|----+AbstractAmazon.php
|----+...more files
|----+SimilarProduct.php
|-+test.php

gist是我创建了一个名为 testing 的目录,我们将放置ZF2自动加载器以及Amazon类,他们的依赖。正在测试的是包含自动加载器(在 Loader )和 ZendService的 Zend Amazon 服务的文件夹。

The gist is I have created a directory called testing where we will place the ZF2 autoloader as well as the Amazon classes and their dependencies. Under testing is a Zend folder that contains the autoloader (in Loader) and also a ZendService folder where the Amazon service goes.

首先,我们需要从ZF2获取自动加载器的副本。部分原因,你有麻烦,因为它看起来像你使用的ZF1自动加载机是不兼容ZF2。要从ZF2获取Autoloader,您可以下载最新的ZF2软件包,并将 Loader 目录从 ZendFramework-2.0.x / library / Zend / 导入 Zend test

First, we need to get a copy of the autoloader from ZF2. Part of the reason you were having trouble is because it looks like you are using the ZF1 autoloader which is incompatible with ZF2. To get the Autoloader from ZF2, you can download the latest ZF2 package and copy the Loader directory from ZendFramework-2.0.x/library/Zend/ into the Zend folder we created in the testing directory.

现在我们有了自动加载器文件,让我们抓住亚马逊服务文件。我将详细解答如何使用Composer获取最新的软件包,但现在我将解释如何手动获取它。要获取可用的ZF2软件包的完整列表,请在 http://packages.zendframework.com上载入JSON文件/packages.json 在它中找到 zendframework / zendservice-amazon ,确定列表中可用的最高版本,并抓取相应的dist。 EDIT 自2013年7月11日起,这是最新的 zendservice-amazon 包中。

Now that we have the autoloader files, let's grab the Amazon service files. I'll write a detailed answer on how to use Composer to get the latest package, but for now I'll explain how to get it manually. To get the full list of available ZF2 packages, load up the JSON file at http://packages.zendframework.com/packages.json In it find, zendframework/zendservice-amazon, determine the highest version available from the list, and grab the corresponding dist. EDIT As of 11th Jul 2013, this is the latest zendservice-amazon package.

中的目录ZendService_Amazon-2.0.2 .zip ,将整个 ZendService 目录复制到 testing 目录中。您现在已有ZF2亚马逊服务文件。

From the library directory in ZendService_Amazon-2.0.2.zip, copy the entire ZendService directory into the testing directory. You now have the ZF2 Amazon service files.

接下来,照顾依赖关系。从ZF2库中复制目录 Crypt Escaper Http I18n Json Stdlib code>目录中的 Uri 插入到 Zend $ c> testing

Next, take care of the dependencies. From the ZF2 library, copy the directories, Crypt, Escaper, Http, I18n, Json, Stdlib, Uri, and Validator into the Zend directory inside testing.

您还需要 ZendRest 包。将ZendRest包中的 ZendRest 文件夹从复制到 testing / ZendRest

You will also need the ZendRest package. Copy the ZendRest folder from library from the ZendRest package to testing/ZendRest.

现在查看一些代码。在具有以下内容的测试文件夹中创建 test.php

Now for some code. Create test.php inside the testing folder with the following contents:

<?php

require_once './Zend/Loader/StandardAutoloader.php';

$autoloader = new Zend\Loader\StandardAutoloader(array(
    'namespaces' => array(
        'Zend'        => dirname(__FILE__) . '/Zend',
        'ZendRest'    => dirname(__FILE__) . '/ZendRest',
        'ZendService' => dirname(__FILE__) . '/ZendService',
     ),
     'fallback_autoloader' => true));

$autoloader->register();

$tag       = 'prdesign-20'; // replace with your Amazon app ID
$appId     = '1JT2V3QNEHDAMKYR5F02'; // replace w/ your access key from https://portal.aws.amazon.com/gp/aws/securityCredentials
$secretKey = 'Qgjeiw39f8UNzjJgeerrgDs1a193du/v7djDAtn/x';

$query  = new ZendService\Amazon\Query($appId, 'US', $secretKey);
$query->Category('Books')->Keywords('PHP')->AssociateTag($tag);
$result = $query->search();

foreach($result as $item): ?>
<div class="item">
  <a href="<?php echo $item->DetailPageURL ?>" target="_blank"><?php echo $item->Title ?></a>
  by <?php if (is_array($item->Author)): ?>
  <?php echo implode(', ', $item->Author) ?>
  <?php else: ?>
  <?php echo $item->Author ?>
  <?php endif; ?>
</div>
<?php endforeach; ?>

首先,我们 require_once $ c> StandardAutoloader 类从ZF2。一旦自动加载器被注册,这是唯一必须手动包括的类。

First, we require_once the StandardAutoloader class from ZF2. Once the autoloader is registered, this is the only class you have to manually include.

接下来,我们创建一个新的自动加载器并传递一些选项。这告诉自动加载器,其中位于命名空间 Zend ZendService 中的类。我们告诉自动加载器他们住在我们当前目录中的相应文件夹。根据需要将 dirname(__ FILE __)更改为正确的路径。 fallback_autoloader 选项指示自动加载器在 include_path 中查找任何命名空间或供应商的类。

Next, we create a new autoloader and pass some options. This tells the autoloader where classes in the namespace Zend and ZendService are located. We tell the autoloader they live in the respective folders in our current directory. Change dirname(__FILE__) to the correct path as needed. The fallback_autoloader option tells the autoloader to look for classes of any namespace or vendor in the include_path.

然后调用 $ autoloader-> register(); ,然后注册我们在PHP中设置的自动加载器。

Calling $autoloader->register(); then actually registers the autoloader we set up within PHP. Setting up the autoloader is now finished.

接下来的3行为API定义了一些必要的参数。

The next 3 lines define some required parameters for the API.

接下来的3行很简单,我们现在创建一个 ZendService\Amazon\Query 的新实例,并传递我们的Amazon应用程序ID和密钥。然后,通过在Books Category 中指定搜索并将关键字设置为PHP来构建查询。我们还添加了必需的AssociateTag。最后,我们执行搜索。

The next 3 lines are straightforward, we now create a new instance of ZendService\Amazon\Query and pass our Amazon app ID and secret key. We then build the query by specifying to search in the Books Category and set the Keywords to be PHP. We also add the required AssociateTag. Finally, we execute the search.

尚未使用 ZendService\Amazon 不能提供使用类的详细说明,但包括的演示脚本应该让你开始向亚马逊发送基本查询和处理结果。

I haven't yet used ZendService\Amazon yet so I can't provide detailed instructions on using the class but the included demo script should get you started with sending basic queries to Amazon and processing the results.

希望有所帮助。

这篇关于如何使用Zend服务Amazon?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆