Example 139: Link to an Amazon.com Web Service Using SOAP (PHP)
| Description: |
This WebSmart PHP example uses Amazon\'s AWI APIs to retrieve book information. |
|
For more information about Amazon\'s APIs please visit www.Amazon.com. |
';
}
PANEL='ListDetails'
{
DESC='Page body';
ITERATIONS='10';
DETAILS='
$prodName
Authors:
List Price: $prodListPrice
Used Price: $prodUsedPrice
|
|
| |
';
}
PANEL='ListFooter'
{
DESC='Page footer';
DETAILS='
-
Start with a "PHP Record Listing" template.
-
In the program\'s ListHeader HTML segment, find the line
<div id="contents"> and add the following HTML code immediately afterwards:
<form name="srchform" action="" method="POST">
<input type="hidden" name="task" value="search">
<table>
<tr>
<td>Search for author/book:</td>
<td> </td>
<td><input type="text" name="book" value=""></td>
<td><input type="Submit" value="Search"></td>
</tr>
<tr>
<td colspan="4" align="center"><b>Search displays first 10 items.</b></td>
</tr>
</table>
</form>
<br>
<table border="1">
-
Replace the HTML code on the \'ListDetails\' segment with the following:
<tr>
<td class="text" width="300">
<a href="\\$prodURL">\\$prodName</a>
<br>
<strong>Authors:</strong> <?php echo(implode(", ", \\$prodAuthors)); ?>
<br><br>
<strong>List Price: $prodListPrice</strong>
<br>
<span style="color: red;">Used Price: \\$prodUsedPrice</span>
</td>
<td class="text" valign="middle" align="center">
<img border="1" src="\\$prodImgURLsm" border="0" alt="book cover">
</td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
-
Replace the PHP source code with the following:
// Global variables
global \\$prodName;
global \\$prodURL;
global \\$prodImgURLsm;
global \\$prodImgURLlg;
global \\$prodListPrice;
global \\$prodUsedPrice;
global \\$prodAuthors;
// Do the task
\\$pf_task = strtolower(\\$pf_task);
switch(\\$pf_task)
{
case \'default\':
mySOAPClient();
break;
case \'search\':
mySOAPClient();
break;
}
// Implement a simple SOAP client to connect to Amazon.com
function mySOAPClient()
{
// Retrieve global variables
foreach(\\$GLOBALS as \\$arraykey=>\\$arrayvalue)
{
if (\\$arraykey[0]!=\'_\' && \\$arraykey != "GLOBALS")
global \\$\\$arraykey;
}
// Retrieve search parameter
if (isset(\\$_REQUEST[\'book\']))
\\$keyword = \\$_REQUEST[\'book\'];
else
\\$keyword = \'\';
if(\\$keyword == \'\')
{
wrtseg(\'ListHeader\');
wrtseg(\'ListFooter\');
return;
}
else
{
// Create SoapClient objects in WSDL mode.
\\$client =
new SoapClient("http://soap.amazon.com/schemas2/AmazonWebServices.wsdl");
\\$params = array(
\'keyword\' => \\$keyword,
\'page\' => 1,
\'mode\' => \'books\',
\'tag\' => \'preinheimerco-20\',
\'type\' => \'lite\',
\'devtag\' => \'1MV0BNXEHBAQ0BJTBQR2\'
);
wrtseg(\'ListHeader\');
// Utilize Amazon\'s AWS API method calls to retrieve the information
// For more information refer to http://www.amazon.com/webservices
// Here is a brief list:
// BrowseNodeSearchRequest() - retrieve a list of catalog items attached
// to a particular node in the Amazon database;
// ASINSearchRequest() - retrieve detailed information for a given
// product code;
// KeywordSearchRequest() - perform a keyword search on the Amazon database;
// SellerSearchRequest() - perform a search for products listed by
// third-party sellers;
// PowerSearchRequest() - perform an advanced search on the Amazon
// database;
// SimilaritySearchRequest() - perform a search for similar items, given a
// specific product code.
try{
\\$result = \\$client->KeywordSearchRequest(\\$params);
}
catch(Exception \\$e)
{
echo(\'Message: \' . \\$e->getMessage());
exit;
}
// The resulting array will contain the following elements:
//
// [Url]
// [Asin]
// [ProductName]
// [Catalog]
// [Authors]
// [ReleaseDate]
// [Manufacturer]
// [ImageUrlSmall]
// [ImageUrlMedium]
// [ImageUrlLarge]
// [ListPrice]
// [UsedPrice]
// [Availability]
//
foreach (\\$result->Details as \\$product)
{
\\$prodName = \\$product->ProductName;
\\$prodURL = \\$product->Url;
\\$prodImgURLsm = \\$product->ImageUrlSmall;
\\$prodImgURLlg = \\$product->ImageUrlLarge;
\\$prodListPrice = \\$product->ListPrice;
\\$prodUsedPrice = \\$product->UsedPrice;
\\$prodAuthors = \\$product->Authors;
wrtseg(\'ListDetails\');
}
wrtseg(\'ListFooter\');
}
}
--------------------------------------------------------------------------------
Notice the bolded text above. You will have to create a key for your own development purposes.
Please visit \'http://www.amazon.com/webservices\' for more information.
-
Compile and run your program.
|