This section shows you how to implement an ItemSearch request in various programming languages.
![]() | 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 Show Language list 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. If you have not downloaded the client side library using wsimport and generated the stubs, refer to the Using the Amazon Associates Web Service Java Client Side Library section before trying to 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("2008-04-07");
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. |
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 Amazon Associates Web Service service
AWSECommerceService ecs = new AWSECommerceService();
// Create ItemSearch wrapper
ItemSearch search = new ItemSearch();
search.AssociateTag = "[Your Associate ID]";
search.AWSAccessKeyId = "[Your ID]";
search.Version = "2008-04-07";
// Create a request object
ItemSearchRequest request = new ItemSearchRequest();
// Fill 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 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://ecs.amazonaws.com/onca/xml";
my $service = "AWSECommerceService";
my $accesskey = "[INSERT YOUR ACCESS KEY ID HERE]";
my $operation = "ItemSearch";
my $version = "2008-04-07";
# Assemble the REST request URL.
my $request =
"$EndPoint?" .
"Service=$service&" .
"AWSAccessKeyId=$accesskey&" .
"Operation=$operation&" .
"Keywords=$keywords&" .
"SearchIndex=$searchIndex&" .
"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 = "2008-04-07";
$ResponseGroup = "ItemAttributes,Offers";
//User interface provides values
//for $SearchIndex and $Keywords
//Define the request
$request=
"http://ecs.amazonaws.com/onca/xml"
. "?Service=AWSECommerceService"
. "&AssociateTag=" . Associate_tag
. "&AWSAccessKeyId=" . Access_Key_ID
. "&Operation=" . $Operation
. "&Version=" . $Version
. "&SearchIndex=" . $SearchIndex
. "&Keywords=" . $Keywords
. "&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, Amazon Associates Web Service 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.