How to use Amazon's Zend Service?

I do a lot of digging and ended up with a huge headache and success. I'm trying to use the Zend Amazon Service (as part of Codeigniter ) to get book information using my ISBN. At first I tried it with Zend 1.12, but I kept getting an error about the missing AssociateTag parameter. Now I'm trying with Zend 2.0, but still getting problems.

This is the code that I use in 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'); 

I get the following error:

Fatal error: class "Zend_Loader_Autoloader" was not found.

My questions:

  • How to download the correct autoloader?
  • Where to include the associated tag in the parameters? This is not mentioned in the outdated zend user manual.
  • Zend 2.0 does not even have amazon utility files or even the / service folder. What does it mean?
+4
source share
2 answers

Note. The demo included in zendservice-amazon does not work as it is. Requests should include your application identifier, secret key, and associated tag, which the demo script does not execute by default. It took me a while to figure this out, without them all requests throw an exception, because the status of the HTTP response is 400. Unfortunately, the exception does not have a response body that indicates which parameters are missing.

Here is the code to help you get started with ZF2 and ZendService\Amazon .

To get started, let me describe the directory structure where I will put the 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 

The bottom line is that I created a directory called testing , where we will place the ZF2 autoloader, as well as Amazon classes and their dependencies. When testing, there is the Zend folder, which contains the autoloader (in Loader ), as well as the ZendService folder where the Amazon service is running.

First we need to get a copy of the autoloader from ZF2. The reason you are having problems 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/ to Zend , which we created in the testing directory.

Now that we have the autoloader files, let me capture Amazon service files. I will write a detailed answer on how to use Composer to get the latest package, but for now I will explain how to get it manually. For a complete list of available ZF2 packages, download the JSON file at http://packages.zendframework.com/packages.json Find the zendframework/zendservice-amazon , determine the highest version available from the list, and grab the corresponding dist. EDIT Since July 11, 2013, this is the latest zendservice-amazon package.

In the library directory in ZendService_Amazon-2.0.2.zip copy the entire ZendService directory to the testing directory. You now have Amazon ZA2 service files.

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

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

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; ?> 

First, we require_once the StandardAutoloader class from ZF2. After registering the autoloader, this is the only class that you need to manually enable.

Then we create a new autoloader and pass some parameters. This tells the autoloader where the classes are located in the Zend and ZendService . We tell the autoloader that they live in the appropriate folders in our current directory. Change dirname(__FILE__) to the correct path as needed. The fallback_autoloader option tells the autoloader to search for classes of any namespace or provider in the include_path .

Call $autoloader->register(); then it actually registers the autoloader that we installed in PHP. Autoloader setup completed.

The following 3 lines define some of the required parameters for the API.

The next 3 lines are simple, now we create a new instance of ZendService\Amazon\Query and pass our Amazon application ID and secret key. Then we create a query by pointing to search in the Category book and set Keywords as PHP. We also add the necessary AssociateTag. Finally, we do a search.

I have not used ZendService\Amazon yet, but I cannot provide detailed instructions for using this class, but the included demonstration script should start by sending basic requests to Amazon and processing the results.

Hope this helps.

+4
source

I am using an Amazon AWS S3 bucket with the Zend Framework. A simple example is uploading photos to Amazon AWS Bucket. In application/config/application.ini file

 ;AWSAccessKeyId= "AccessKey" ;AWSSecretKey= "SecretKey" ;AWSS3BucketName = "zoshare-images" ;AWSS3GetImageUrl = "http://[name].s3.amazonaws.com/[folder]/" 

Suppose you want to upload an image to the trash, below is an example of a controller code.

 public function updateAction() { if($_FILES["fuPic"]["size"] > 0 || $_FILES["fuPre"]["size"] > 0) { $config = Zend_Registry::get('config'); $s3 = new Zend_Service_Amazon_S3($config['AWSAccessKeyId'],$config['AWSSecretKey']); $bucketName = $config['AWSS3BucketName']; if($_FILES["fuPic"]["size"] == 0) unset($_FILES["fuPic"]); else if($_FILES["fuPre"]["size"] == 0) unset($_FILES["fuPre"]); $s3->removeObject($bucketName."/[folder]/".$campaignModel->getCampaignId().'_pic_'.$campaign_olddata->getPicture()); $s3->removeObject($bucketName."/[folder]/".$campaignModel->getCampaignId().'_pre_'.$campaign_olddata->getPreview()); if (isset($_FILES["fuPic"])) { $filename = $campaignModel->getCampaignId().'_pic_'.$_FILES["fuPic"]['name']; $s3->putObject($bucketName."/[folder]/".$filename, file_get_contents($_FILES["fuPic"]["tmp_name"]), array(Zend_Service_Amazon_S3::S3_ACL_HEADER => Zend_Service_Amazon_S3::S3_ACL_PUBLIC_READ)); $data['picture'] = $_FILES["fuPic"]['name']; } if (isset($_FILES["fuPre"])) { $filename = $campaignModel->getCampaignId().'_pre_'.$_FILES["fuPre"]['name']; $s3->putObject($bucketName."/[folder]/".$filename, file_get_contents($_FILES["fuPre"]["tmp_name"]), array(Zend_Service_Amazon_S3::S3_ACL_HEADER => Zend_Service_Amazon_S3::S3_ACL_PUBLIC_READ)); $data['preview'] = $_FILES["fuPre"]['name']; } } } 

Happy coding !!!!

+1
source

Source: https://habr.com/ru/post/1440666/


All Articles