Skip to content
This repository has been archived by the owner on Mar 14, 2021. It is now read-only.

Made some small refactoring stuff #1

Merged
3 commits merged into from
Oct 14, 2010
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
88 changes: 55 additions & 33 deletions lib/AmazonECS.class.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

/**
* Amazon ECS Class
* http://www.amazon.com
Expand All @@ -17,26 +16,25 @@ class AmazonECS
{
/**
* Baseconfigurationstorage

* @var array
*/
private $requestConfig = array();

/**
* Pattern for GET header
*
* Enter description here ...
* @var $requestHeaderPattern
*/
private $requestHeaderPattern = "GET\n%HOST%\n/onca/xml\n";

/**
* Enter description here ...
* @param $accessKey
* @param $secretKey
* @param $requestHost
* @param string $accessKey
* @param string $secretKey
*/
public function __construct($accessKey, $secretKey)
{
if(empty($accessKey) || empty($secretKey))
if (empty($accessKey) || empty($secretKey))
{
throw new Exception('No Access Key or Secret Key has been set');
}
Expand All @@ -47,13 +45,12 @@ public function __construct($accessKey, $secretKey)

public function search($pattern)
{

if(!isset($this->requestConfig['category']))
if (false === isset($this->requestConfig['category']))
{
throw new Exception('No Category given: Please set it up before');
}

$params = array(
$params = array(
'AWSAccessKeyId' => $this->requestConfig['accessKey'],
'Request' => array(
'Operation' => 'ItemSearch',
Expand All @@ -62,46 +59,71 @@ public function search($pattern)
);

$this->performSoapRequest("ItemSearch", $params);

return $this;
}

public function category($category)
public function category($category = null)
{
if (null === $category)
{
return isset($this->requestConfig['category']) ? $this->requestConfig['category'] : null;
}

$this->requestConfig['category'] = $category;

return $this;
}

protected function performSoapRequest($function, $params )
protected function performSoapRequest($function, $params)
{
$timeStamp = $this->getTimestamp();
$signature = $this->buildSignature($function.$timeStamp);

$soapHeader = array(
new SoapHeader('http://security.amazonaws.com/doc/2007-01-01/', 'AWSAccessKeyId', $this->requestConfig['accessKey']),
new SoapHeader('http://security.amazonaws.com/doc/2007-01-01/', 'Timestamp', $timeStamp),
new SoapHeader('http://security.amazonaws.com/doc/2007-01-01/', 'Signature', $signature)
$soapClient = new SoapClient(
'http://ecs.amazonaws.com/AWSECommerceService/2010-09-01/DE/AWSECommerceService.wsdl',
array('exceptions' => 0)
);
$soapClient->__setSoapHeaders($this->builSoapHeader($function));

$soapClient = new SoapClient('http://ecs.amazonaws.com/AWSECommerceService/2010-09-01/DE/AWSECommerceService.wsdl', array('exceptions' => 0));
$soapClient->__setSoapHeaders($soapHeader);

$res = $soapClient->__soapCall($function, array($params));
return $soapClient->__soapCall($function, array($params));
}

var_dump($res);
/**
* Provides some necessary soap headers
*
* @param string $function
*
* @return array Each element is a concrete SoapHeader object
*/
protected function buildSoapHeader($function)
{
$timeStamp = $this->getTimestamp();
$signature = $this->buildSignature($function . $timeStamp);

return array(
new SoapHeader(
'http://security.amazonaws.com/doc/2007-01-01/',
'AWSAccessKeyId',
$this->requestConfig['accessKey']
),
new SoapHeader(
'http://security.amazonaws.com/doc/2007-01-01/',
'Timestamp',
$timeStamp
),
new SoapHeader(
'http://security.amazonaws.com/doc/2007-01-01/',
'Signature',
$signature
)
);
}

protected function getTimestamp()
final protected function getTimestamp()
{
return gmdate("Y-m-d\TH:i:s\Z");
}

protected function buildSignature($request)
final protected function buildSignature($request)
{
return base64_encode(hash_hmac("sha256", $request, $this->requestConfig['secretKey'], True));
return base64_encode(hash_hmac("sha256", $request, $this->requestConfig['secretKey'], true));
}


}

?>
14 changes: 9 additions & 5 deletions test.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
<pre>
<?php
if ("cli" !== PHP_SAPI)
{
echo "<pre>";
}

require 'lib/AmazonECS.class.php';


try {
$test = new AmazonECS("", "");
try
{
$test = new AmazonECS("", "");
$test->category('DVD')->search("Matrix Revolutions");
}
catch(Exception $e)
{
echo $e->getMessage();
}
$test->category('DVD')->search("Matrix Revolutions");

?>