Skip to content

Commit

Permalink
Fix #14 - invalid ASIN array (#16)
Browse files Browse the repository at this point in the history
Throw errors on invalid XML
  • Loading branch information
MarcL authored Nov 6, 2017
1 parent 9ac01cf commit 987f5d9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 15 deletions.
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 987f5d9

Please sign in to comment.