· 9 years ago · Sep 01, 2016, 09:58 AM
1function mkm_baseURL(){
2 $baseURL = "https://www.mkmapi.eu/ws/v2.0/output.json/";
3 return ($baseURL);
4}
5
6function mkm_SetAuthHeaderParams($setMethod='GET'){
7 $r['method'] = $setMethod;
8 $r['appToken'] = "kWRiG29XBiGo3rru";
9 $r['appSecret'] = "b6d6uQgHAh6RkwxxGfnWgT6wW7XmKMp2";
10 $r['accessToken'] = NULL;
11 $r['accessSecret'] = NULL;
12 $r['nonce'] = uniqid();
13 $r['timestamp'] = time();
14 $r['signatureMethod'] = "HMAC-SHA1";
15 $r['version'] = "1.0";
16 return($r);
17}
18
19function mkm_APICall($endpoint,$queryParams=null){
20 ksort($queryParams);
21 $url= mkm_baseURL() . $endpoint;
22
23 $AuthHeaderParams = mkm_SetAuthHeaderParams('GET');
24
25 extract($AuthHeaderParams);
26 $realmArray = explode("?",$url);
27 $realm = $realmArray[0];
28
29 /**
30 * Gather all parameters that need to be included in the Authorization header and are know yet
31 *
32 * @var $params array|string[] Associative array of all needed authorization header parameters
33 */
34 $params = array(
35 'realm' => $realm,
36 'oauth_consumer_key' => $appToken,
37 'oauth_token' => $accessToken,
38 'oauth_nonce' => $nonce,
39 'oauth_timestamp' => $timestamp,
40 'oauth_signature_method' => $signatureMethod,
41 'oauth_version' => $version
42 );
43 //print_r($params);
44 /**
45 * Start composing the base string from the method and request URI
46 *
47 * @var $baseString string Finally the encoded base string for that request, that needs to be signed
48 */
49 $baseString = strtoupper($method) . "&";
50 $baseString .= rawurlencode($realm) . "&";
51
52 //vd("Base String, pre Params: ".$baseString);
53 /*
54 * Gather, encode, and sort the base string parameters
55 */
56 $encodedParams = array();
57 foreach ($params as $key => $value)
58 {
59 if ("realm" != $key AND "query_parameters" != $key)
60 {
61 $encodedParams[rawurlencode($key)] = rawurlencode($value);
62 }
63 if ("query_parameters" == $key){
64 $encodedParams[$key] = $value;
65 }
66 }
67 ksort($encodedParams);
68 echo "<hr>ksort'ed $encodedParams";
69 //vd($encodedParams);
70 /*
71 * Expand the base string by the encoded parameter=value pairs
72 */
73 $values = array();
74 foreach ($queryParams as $key=>$value){
75 $values[] = $key . "=" . $value;
76 }
77
78 foreach ($encodedParams as $key => $value)
79 {
80 $values[] = $key . "=" . $value;
81 }
82 $paramsString = rawurlencode(implode("&", $values));
83 $baseString .= $paramsString;
84 //vd("Base String, Post Params: ".$baseString);
85 /*
86 * Create the signingKey
87 */
88 $signatureKey = rawurlencode($appSecret) . "&" . rawurlencode($accessSecret);
89
90 /**
91 * Create the OAuth signature
92 * Attention: Make sure to provide the binary data to the Base64 encoder
93 *
94 * @var $oAuthSignature string OAuth signature value
95 */
96 $rawSignature = hash_hmac("sha1", $baseString, $signatureKey, true);
97 $oAuthSignature = base64_encode($rawSignature);
98
99 /*
100 * Include the OAuth signature parameter in the header parameters array
101 */
102 $params['oauth_signature'] = $oAuthSignature;
103
104 /*
105 * Construct the header string
106 */
107 $header = "Authorization: OAuth ";
108 $headerParams = array();
109 foreach ($params as $key => $value)
110 {
111 $headerParams[] = $key . "=\"" . $value . "\"";
112 }
113 $header .= implode(", ", $headerParams);
114
115 /*
116 * Get the cURL handler from the library function
117 */
118 $curlHandle = curl_init();
119
120 /*
121 * Set the required cURL options to successfully fire a request to MKM's API
122 *
123 * For more information about cURL options refer to PHP's cURL manual:
124 * http://php.net/manual/en/function.curl-setopt.php
125 */
126 curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
127 curl_setopt($curlHandle, CURLOPT_URL, $url);
128 curl_setopt($curlHandle, CURLOPT_HTTPHEADER, array($header));
129 curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, false);
130
131 /**
132 * Execute the request, retrieve information about the request and response, and close the connection
133 *
134 * @var $content string Response to the request
135 * @var $info array Array with information about the last request on the $curlHandle
136 */
137 $content = curl_exec($curlHandle);
138 $info = curl_getinfo($curlHandle);
139 //echo "<hr>HTTP Response Header:";
140 //vd($info);
141 curl_close($curlHandle);
142
143 /*
144 * Convert the response string into an object
145 *
146 * If you have chosen XML as response format (which is standard) use simplexml_load_string
147 * If you have chosen JSON as response format use json_decode
148 *
149 * @var $decoded \SimpleXMLElement|\stdClass Converted Object (XML|JSON)
150 */
151 return ($content);
152}
153
154function mkm_getCardPrice($cardID=211116,$minCondition="NM",$start=0,$maxResults=10,$langID=1){
155 $endpoint = "articles/".$cardID."?userType=private&idLanguage=".$langID."&minCondition=".$minCondition."&start=".$start."&maxResults=".$maxResults."";
156 $queryParams = array(
157 rawurlencode('userType')=>rawurlencode('private'),
158 rawurlencode('idLanguage')=>rawurlencode($langID),
159 rawurlencode('minCondition')=>rawurlencode($minCondition),
160 rawurlencode('start')=>rawurlencode($start),
161 rawurlencode('maxResults')=>rawurlencode($maxResults)
162 );
163 $contents = mkm_APICall($endpoint,$queryParams);
164 return($contents);
165}
166
167echo mkm_getCardPrice();