Skip to content

Commit

Permalink
Fix #14 - invalid ASIN array
Browse files Browse the repository at this point in the history
Throw errors on invalid XML
  • Loading branch information
Marc Littlemore committed Nov 6, 2017
1 parent 9ac01cf commit 899a165
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 15 deletions.
8 changes: 5 additions & 3 deletions composer.json
@@ -1,7 +1,8 @@
{
"name": "marcl/amazonproductapi",
"description": "PHP library to perform product lookup and searches using the Amazon Product API.",
"version": "3.0.1",
"description":
"PHP library to perform product lookup and searches using the Amazon Product API.",
"version": "3.0.2",
"type": "library",
"keywords": ["amazon", "product", "api"],
"homepage": "https://github.com/MarcL/AmazonProductAPI/",
Expand Down Expand Up @@ -31,4 +32,5 @@
"MarcL\\": "src/",
"tests\\": "tests/"
}
}}
}
}
9 changes: 9 additions & 0 deletions examples.php
Expand Up @@ -48,4 +48,13 @@
$items = $amazonAPI->ItemLookUp('B01GAGVIE4', true);
print('>> Look up specific ASIN\n');
var_dump($items);

sleep($sleepTime);

// Amazon echo, lookup with incorrect ASIN array
$asinIds = array('INVALID', 'INVALIDASIN', 'NOTANASIN');
$items = $amazonAPI->ItemLookUp($asinIds, true);
print('>> Look up specific ASIN\n');
var_dump($items);
var_dump($amazonAPI->GetErrors());
?>
11 changes: 6 additions & 5 deletions src/AmazonAPI.php
Expand Up @@ -135,15 +135,16 @@ private function MakeAndParseRequest($params) {
$response = $request->execute($signedUrl);

$parsedXml = simplexml_load_string($response);

if ($parsedXml === false) {
return false;
}

return $this->dataTransformer->execute($parsedXml);
} catch(\Exception $error) {
$this->AddError("Error downloading data : $signedUrl : " . $error->getMessage());
}

if ($parsedXml === false) {
return false;
}

return $this->dataTransformer->execute($parsedXml);
}
}
?>
12 changes: 5 additions & 7 deletions src/Transformers/SimpleArrayTransformer.php
Expand Up @@ -8,21 +8,18 @@ class SimpleArrayTransformer implements IDataTransformer {
public function execute($xmlData) {
$items = array();
if (empty($xmlData)) {
$this->AddError("No XML response found from AWS.");
return($items);
throw new \Exception("No XML response found from AWS.");
}

if (empty($xmlData->Items)) {
$this->AddError("No items found.");
return($items);
}

if ($xmlData->Items->Request->IsValid != 'True') {
$errorCode = $xmlData->Items->Request->Errors->Error->Code;
$errorMessage = $xmlData->Items->Request->Errors->Error->Message;
$error = "API ERROR ($errorCode) : $errorMessage";
$this->AddError($error);
return($items);
throw new \Exception($error);
}

// Get each item
Expand All @@ -48,7 +45,8 @@ public function execute($xmlData) {
array_push($items, $item);
}

return($items); }
return($items);
}
}

?>
?>

0 comments on commit 899a165

Please sign in to comment.