Skip to content

Commit 987f5d9

Browse files
authored
Fix #14 - invalid ASIN array (#16)
Throw errors on invalid XML
1 parent 9ac01cf commit 987f5d9

File tree

4 files changed

+25
-15
lines changed

4 files changed

+25
-15
lines changed

composer.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"name": "marcl/amazonproductapi",
3-
"description": "PHP library to perform product lookup and searches using the Amazon Product API.",
4-
"version": "3.0.1",
3+
"description":
4+
"PHP library to perform product lookup and searches using the Amazon Product API.",
5+
"version": "3.0.2",
56
"type": "library",
67
"keywords": ["amazon", "product", "api"],
78
"homepage": "https://github.com/MarcL/AmazonProductAPI/",
@@ -31,4 +32,5 @@
3132
"MarcL\\": "src/",
3233
"tests\\": "tests/"
3334
}
34-
}}
35+
}
36+
}

examples.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,13 @@
4848
$items = $amazonAPI->ItemLookUp('B01GAGVIE4', true);
4949
print('>> Look up specific ASIN\n');
5050
var_dump($items);
51+
52+
sleep($sleepTime);
53+
54+
// Amazon echo, lookup with incorrect ASIN array
55+
$asinIds = array('INVALID', 'INVALIDASIN', 'NOTANASIN');
56+
$items = $amazonAPI->ItemLookUp($asinIds, true);
57+
print('>> Look up specific ASIN\n');
58+
var_dump($items);
59+
var_dump($amazonAPI->GetErrors());
5160
?>

src/AmazonAPI.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,16 @@ private function MakeAndParseRequest($params) {
135135
$response = $request->execute($signedUrl);
136136

137137
$parsedXml = simplexml_load_string($response);
138+
139+
if ($parsedXml === false) {
140+
return false;
141+
}
142+
143+
return $this->dataTransformer->execute($parsedXml);
138144
} catch(\Exception $error) {
139145
$this->AddError("Error downloading data : $signedUrl : " . $error->getMessage());
140-
}
141-
142-
if ($parsedXml === false) {
143146
return false;
144147
}
145-
146-
return $this->dataTransformer->execute($parsedXml);
147148
}
148149
}
149150
?>

src/Transformers/SimpleArrayTransformer.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,18 @@ class SimpleArrayTransformer implements IDataTransformer {
88
public function execute($xmlData) {
99
$items = array();
1010
if (empty($xmlData)) {
11-
$this->AddError("No XML response found from AWS.");
12-
return($items);
11+
throw new \Exception("No XML response found from AWS.");
1312
}
1413

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

2018
if ($xmlData->Items->Request->IsValid != 'True') {
2119
$errorCode = $xmlData->Items->Request->Errors->Error->Code;
2220
$errorMessage = $xmlData->Items->Request->Errors->Error->Message;
2321
$error = "API ERROR ($errorCode) : $errorMessage";
24-
$this->AddError($error);
25-
return($items);
22+
throw new \Exception($error);
2623
}
2724

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

51-
return($items); }
48+
return($items);
49+
}
5250
}
5351

54-
?>
52+
?>

0 commit comments

Comments
 (0)