Menu
Product Advertising API
Getting Started Guide (API Version 2013-08-01)

Implementing a Product Advertising API Request

This section shows you how to implement an ItemSearch request in various programming languages. A SearchIndex is similar to a product category, such as Books, Automobile, or Jewelry. Keywords is a word or phrase. The request selects items in the specified search index that have the Keywords value in their title or description. These examples don't work as is because you must include valid request authentication. You may find the following links helpful as you learn more about Product Advertising API and making requests.

Note

If you are viewing this document online, you can use the Filter View drop-down list in the top-right corner of the page to view the example code in your programming language only.

Java

The following Java code implements an ItemSearch request in which the customer enters values for SearchIndex and Keywords. This example uses the Java client-side library to simplify the implementation of the request. To download the client-side library using wsimport and generate the stubs, see Java Setup and use the following code.

Copy
// Set the service: com.ECS.client.jax.AWSECommerceService service = new com.ECS.client.jax.AWSECommerceService(); //Set the service port: com.ECS.client.jax.AWSECommerceServicePortType port = service.getAWSECommerceServicePort(); //Get the operation object: com.ECS.client.jax.ItemSearchRequest itemRequest = new com.ECS.client.jax.ItemSearchRequest(); //Fill in the request object: itemRequest.setSearchIndex("Books"); itemRequest.setKeywords("dog"); itemRequest.setVersion("2013-08-01"); com.ECS.client.jax.ItemSearch ItemElement= new com.ECS.client.jax.ItemSearch(); ItemElement.setAWSAccessKeyId("[YOUR ID]"); ItemElement.getRequest().add(itemRequest); //Call the Web service operation and store the response //in the response object: com.ECS.client.jax.ItemSearchResponse response = port.itemSearch(ItemElement);

C#

The following C# code implements an ItemSearch request in which the customer enters values for SearchIndex and Keywords. Comments are inline.

Note

The GettingStartedGuideSample.com.amazonaws.ecs package is generated automatically when you use the .NET "Add Web Reference" dialog box.

Copy
using System; using System.Collections.Generic; using System.Text; using GettingStartedGuideSample.com.amazonaws.ecs; namespace GettingStartedGuideSample { class Program { static void Main(string[] args) { // Set default args if two are not supplied if (args.Length != 2) { args = new string[] { "DVD", "Matrix" }; } // Get searchIndex and keywords from the command line string searchIndex = args[0]; string keywords = args[1]; // Create an instance of the Product Advertising API service AWSECommerceService ecs = new AWSECommerceService(); // Create an ItemSearch wrapper ItemSearch search = new ItemSearch(); search.AssociateTag = "[Your Associate ID]"; search.AWSAccessKeyId = "[Your ID]"; search.Version = "2013-08-01"; // Create a request object ItemSearchRequest request = new ItemSearchRequest(); // Fill the request object with request parameters request.ResponseGroup = new string[] { "ItemAttributes" }; // Set SearchIndex and Keywords request.SearchIndex = searchIndex; request.Keywords = keywords; // Set the request on the search wrapper search.Request = new ItemSearchRequest[] { request }; try { //Send the request and store the response //in response ItemSearchResponse response = ecs.ItemSearch(search);

Perl

The following Perl code implements a Keywords request in which the customer enters values for SearchIndex and Keywords. Comments are inline.

Copy
#!/usr/bin/perl use strict; use warnings; use LWP::UserAgent qw($ua get); use MIME::Base64; use XML::XPath; use Date::Format; # Retrieve command line args for SearchIndex and Keywords die "Usage: $0 <space-separated entry for Search Index and Keywords>\n" unless @ARGV; my $searchIndex = $ARGV[0]; my $keywords = $ARGV[1]; # Define the parameters in the REST request. # Customer cannot change the following values. my $EndPoint = "http://webservices.amazon.com/onca/xml"; my $service = "AWSECommerceService"; my $accesskey = "[INSERT YOUR ACCESS KEY ID HERE]"; my $operation = "ItemSearch"; my $version = "2013-08-01"; # Assemble the REST request URL. my $request = "$EndPoint?" . "Service=$service&" . "AWSAccessKeyId=$accesskey&" . "Operation=$operation&" . "Keywords=$keywords&" . "SearchIndex=$searchIndex&" . "Signature=[Request Signature]&" . "Version=$version" ; # Send the request using HTTP GET. my $ua = new LWP::UserAgent; $ua->timeout(30); my $response = $ua->get($request); my $xml = $response->content;

PHP

The following PHP code implements an ItemSearch request in which the customer enters values for SearchIndex and Keywords. Store this sample code in a file named SimpleStore.php. Comments are inline.

Copy
<?php //Enter your IDs define("Access_Key_ID", "[Your Access Key ID]"); define("Associate_tag", "[Your Associate Tag ID]"); //Set up the operation in the request function ItemSearch($SearchIndex, $Keywords){ //Set the values for some of the parameters $Operation = "ItemSearch"; $Version = "2013-08-01"; $ResponseGroup = "ItemAttributes,Offers"; //User interface provides values //for $SearchIndex and $Keywords //Define the request $request= "http://webservices.amazon.com/onca/xml" . "?Service=AWSECommerceService" . "&AssociateTag=" . Associate_tag . "&AWSAccessKeyId=" . Access_Key_ID . "&Operation=" . $Operation . "&Version=" . $Version . "&SearchIndex=" . $SearchIndex . "&Keywords=" . $Keywords . "&Signature=" . [Request Signature] . "&ResponseGroup=" . $ResponseGroup; //Catch the response in the $response object $response = file_get_contents($request); $parsed_xml = simplexml_load_string($response); printSearchResults($parsed_xml, $SearchIndex); } ?>

The first part of this implementation constructs the ItemSearch request. The first parameters in the list, including the endpoint, the service name, the access key ID, Associate tag, Product Advertising API version number, and operation name, cannot be changed by the customer. The last two parameters, SearchIndex and Keywords, are values set by the customer through the user interface. The last two parameter values are entered by a customer using a web application, for example:

Copy
<table align='left'> <?php print(" <form name='SearchTerms' action=SimpleStore.php method='GET'> <tr><td valign='top'> <b>Choose a Category</b><br> <select name='SearchIndex'> <option value='Books'>Books</option> <option value='DVD'>DVD</option> <option value='Music'>Music</option> </select> </td></tr> <tr><td><b>Enter Keywords</b><br><input type='text' name='Keywords' size='40'/></td></tr> <input type='hidden' name='Action' value='Search'> <input type='hidden' name='CartId' value=$CartId> <input type='hidden' name='HMAC' value=$HMAC> <tr align='center'><td><input type='submit'/></td></tr> </form> "); ?> </table>

This example uses a table to format a web page, which is composed of an HTML form. An HTML select statement provides a drop-down list of value choices for SearchIndex. An HTML input statement provides a text box for the customer to enter a value for Keywords. The request is sent using the PHP command, file_get_contents.

On this page: