| « PreviousNext » | |
![]() ![]() | Did this page help you? Yes | No | Tell us about it... |
This section shows you how to implement an
ItemSearch request in various programming
languages. It's important to note that these examples do not work as is since you must include valid request authentication.
You may find the following links helpful as you learn more about Product Advertising API and making requests.
Complete code examples—Product Advertising API Sample Code & Libraries
Authenticating requests—Request Authentication in the Product Advertising API Developer Guide
Timestamp and Signature—Authentication Parameters in the Product Advertising API Developer Guide.
About Product Advertising API—http://aws.amazon.com/associates/
Another useful tool for trying out various requests is the Product Advertising API Scratchpad. This tool is a web-based application that enables you to quickly submit requests to the Product Advertising API. You can explore basic ItemLookup and ItemSearch requests using REST queries.
Note
If you are viewing this document online, you can view the example code in only one programming language by clicking your preferred language in the Filter View drop-down on the top-right corner of the page.
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.
// 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("2011-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);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 auto-generated when you use the .NET "Add Web Reference…" dialog box.
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 = "2011-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);The following Perl code implements an Keywords
request in which the customer enters values for SearchIndex and
Keywords. Comments are inline.
#!/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 forSearchIndexand 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 = "2011-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;
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.
<?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 = "2011-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 are those that the customer cannot alter, including the endpoint,
the service name, the Access Key ID, Associate Tag, Product Advertising API
version number, and operation name.
The last two parameters, SearchIndex and
Keywords, are values set by the customer
through the user interface. 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.
The last two parameter values are entered by a customer using a web application, for example:
<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 the
Keywords value.
The request is sent using the PHP command, file_get_contents.