· 7 years ago · Oct 17, 2018, 03:46 PM
1<?php
2
3class AmazonAPI {
4
5 var $amazon_aff_id;
6 var $amazon_access_key;
7 var $amazon_secret_key;
8
9 var $url_params;
10 var $itemID;
11 var $xml;
12
13 var $operation;
14 var $signature;
15 var $response_groups = "Small,Images,OfferSummary";
16
17 var $error_message;
18 var $error=0;
19
20
21 public function __construct($affid, $access, $secret)
22 {
23 $this->amazon_aff_id = $affid;
24 $this->amazon_access_key = $access;
25 $this->amazon_secret_key = $secret;
26 }
27
28 public function build_url()
29 {
30 $url = "https://webservices.amazon.de/onca/xml?";
31
32 $this->response_groups = str_replace(",", "%2C", $this->response_groups);
33
34 $url_params = "AWSAccessKeyId=" . $this->amazon_access_key;
35 $url_params .= "&AssociateTag=" . $this->amazon_aff_id;
36
37 if(!empty($this->itemID)) {
38 $url_params .= "&ItemId=" . $this->itemID;
39 }
40
41 $url_params .= "&Operation=" . $this->operation;
42 $url_params .= "&ResponseGroup=" . $this->response_groups;
43 $url_params .= "&Service=AWSECommerceService";
44 $url_params .= "&Timestamp=" . rawurlencode(gmdate("Y-m-d\TH:i:s\Z"));
45 $url_params .= "&Version=2013-08-01";
46
47 $this->url_params = $url_params;
48
49 $url .= $url_params;
50 $url .= "&Signature=" . $this->generate_signature();
51
52 return $url;
53 }
54
55 public function generate_signature()
56 {
57 $this->signature = base64_encode(hash_hmac("sha256",
58 "GET\nwebservices.amazon.de\n/onca/xml\n" . $this->url_params,
59 $this->amazon_secret_key, True));
60 $this->signature = str_replace("+", "%2B", $this->signature);
61 $this->signature = str_replace("=", "%3D", $this->signature);
62 return $this->signature;
63 }
64
65 public function item_lookup($id)
66 {
67 $this->operation = "ItemLookup";
68 $this->itemID = $id;
69
70 $url = $this->build_url();
71
72 $ch = curl_init();
73
74 curl_setopt($ch,CURLOPT_URL,$url);
75 curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
76
77 $output = curl_exec($ch);
78
79 curl_close($ch);
80
81 $this->xml = simplexml_load_string($output);
82 return $this;
83 }
84
85 public function check_for_errors()
86 {
87 if(isset($this->xml->Error)) {
88 $this->error_message = $this->xml->Error->Message;
89 $this->error = 1;
90 }
91 if(isset($this->xml->Items->Request->Errors)) {
92 $this->error_message = $this->xml->Items->Request->Errors->Error->Message;
93 $this->error = 1;
94 }
95 return $this->error;
96 }
97
98 public function get_item_price($product)
99 {
100 $price = 0;
101 if(isset($product->LowestNewPrice)) {
102 $price = $product->LowestNewPrice->Amount;
103 } elseif(isset($product->LowestUsedPrice)) {
104 $price = $product->LowestUsedPrice->Amount;
105 } elseif(isset($product->LowestCollectiblePrice)) {
106 $price = $product->LowestCollectiblePrice->Amount;
107 } elseif(isset($product->LowestRefurbishedPrice)) {
108 $price = $product->LowestRefurbishedPrice->Amount;
109 }
110 return $price;
111 }
112
113 public function get_item_data()
114 {
115 if($this->check_for_errors()) echo "null";
116
117 $product = $this->xml->Items->Item;
118 $item = new STDclass;
119 $item->detailedPageURL = $product->DetailPageURL;
120 $item->link = "https://www.amazon.de/gp/product/".$this->itemID."/?tag=" . $this->amazon_aff_id;
121 $item->title = $product->ItemAttributes->Title;
122 $item->smallImage = $product->SmallImage->URL;
123 $item->mediumImage = $product->MediumImage->URL;
124 $item->largeImage = $product->LargeImage->URL;
125
126 $item->price = $this->get_item_price($product->OfferSummary);
127
128 return $item;
129 }
130
131}
132
133
134
135
136
137
138
139$amazon = new AmazonAPI("associate-id", "access-key", "secret-key");
140$item = $amazon->item_lookup("ASIN")->get_item_data();
141
142echo $item->title;
143
144?>