Sunday, April 15, 2007

Sample PHP Codes Call Amazon Web Services

// callAmazonRESTWS.php
header("Content-type: text/xml");
$base = 'http://webservices.amazon.com/onca/xml';
$query_string = '';
$params = array(
'Service' =>'AWSECommerceService',
'SubscriptionId' => '16XT8ETKKB7NWHAGCQ02' ,
'Operation' => 'ItemSearch',
'SearchIndex' => 'Books',
'Keywords' => 'Web Services');

foreach ($params as $key => $value) {
if ($key != 'Keywords')
$query_string .= "$key=" . urlencode($value) . "&";
else
$query_string .="$key=".urlencode($value);
}
$url = "$base?$query_string";

// Define a context for HTTP.
$aContext = array(
'http' => array(
'proxy' => 'tcp://202.12.97.116:8080', // This is the sample server and the port of the proxys server.
'request_fulluri' => True,
),
);
$cxContext = stream_context_create($aContext);
// Now all file stream functions can use this context.
$xml = file_get_contents($url, False, $cxContext);
echo $xml;
>



You will get the result as shown on this picture.
















References


* Roger L. Costello, “Building Web Services the REST Way”, http://www.xfront.com/REST-Web-Services.html
* Stefan Marr, “RESTful Web Services”, HPI, Seminar Advanced Database Technology
* Pete Freitag, “REST vs SOAP Web Services”, http://www.petefreitag.com/item/431.cfm
* Google, “Google SOAP Search API”, http://code.google.com/apis/soapsearch/
* Sameer Tyagi, “RESTful Web Services”,
http://java.sun.com/developer/technicalArticles/WebServices/restful

No comments: