-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.php
216 lines (151 loc) · 5.67 KB
/
api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
/*/
rjuQSujRZwugWwsx
0QdYZcbjM9zK7qX4x4tSs1ur6NVaBuBG
KuIKWnjG8G6Sv5XBd9XX7mYyq13LoYYG
Nbx1gkL3XFeDH6cVcbdEXqphaHzWrQhK
*/
//getArticleDetails(319033);
//getGames();
//getCardsBySetId("GME", 2373);
function getGames(){
$url = "https://api.cardmarket.com/ws/v2.0/output.json/games";
$data = doConnect($url);
$json = array();
//var_export($data);
//$file = fopen(__DIR__."/mkm/input/games.json", "w");
//fwrite($file, json_encode($data));
//fclose($file);
foreach ($data->game as $game){
echo "#" . $game->idGame . " " . $game->name;
//var_export($game->idGame);
echo "\n";
getAllExpansions(($game->name), strtoupper($game->abbreviation), $game->idGame);
//return;
}
//fwrite($file, json_encode($obj)); fclose($file);
}
function getAllExpansions($name, $code, $id){
$url = "https://api.cardmarket.com/ws/v2.0/output.json/games/".$id."/expansions";
$data = doConnect($url);
$obj = array("expansions" => array());
foreach ($data->expansion as $item){
// var_export($data->expansion); return;
$obj["expansions"][] = array("code" => $code, "name" => $item->enName, "id" => $item->idExpansion);
}
$file = fopen(__DIR__."/mkm/input/" . $code .".json", "w");
fwrite($file, json_encode($obj)); fclose($file);
}
function getArticleDetails($id){
$url = "https://api.cardmarket.com/ws/v2.0/output.json/articles/".$id."?isFoil=1";
//echo $url;
$data = doConnect($url);
//foreach ($data as $key => $value){echo "\n".$key;}
//echo "\n"; var_export($data);
echo "\n results: ".sizeof($data); return;
//var_dump($data); return;
//$export = array("name" =>
$file = fopen(__DIR__."/mkm/input/" . $set->code .".json", "w");
fwrite($file, json_encode($data->single));
fclose($file);
//fwrite($file, json_encode($obj)); fclose($file);
}
function getMKMJson($code){
$set = getSetByCode($code);
getCardsBySetId($set);
}
function getCardsBySetId($code, $id){
$url = "https://api.cardmarket.com/ws/v2.0/output.json/expansions/".$id."/singles";
$json = doConnect($url);
$export = array("name" => "", "code" => $code, "mkm_name" => "", "cards" => array());
//var_export($json); return;
foreach ($json->single as $entry){
//var_export($entry->enName); ;
$export["cards"][] = array("name" => $entry->enName, "rarity" => $entry->rarity);
}
//$file = fopen(__DIR__."/mkm/input/" . $set->code .".json", "w");
$file = fopen(__DIR__."/mkm/input/".$code.".json", "w");
fwrite($file, json_encode($export));
fclose($file);
//fwrite($file, json_encode($obj)); fclose($file);
}
function getSetByCode($code){
$json = json_decode(file_get_contents(__DIR__."/mkm/expansions.json"));
for ($i = 0; $i < sizeof($json->expansions); $i++){
//echo $json->expansions[$i]->code."\n";
if ($json->expansions[$i]->code == $code){
return $json->expansions[$i];
}
}
}
function doConnect($url){
// Declare and assign all needed variables for the request and the header
$method = "GET";
$appToken = "rjuQSujRZwugWwsx";
$appSecret = "0QdYZcbjM9zK7qX4x4tSs1ur6NVaBuBG";
$accessToken = "KuIKWnjG8G6Sv5XBd9XX7mYyq13LoYYG";
$accessSecret = "Nbx1gkL3XFeDH6cVcbdEXqphaHzWrQhK";
$nonce = uniqid();
$timestamp = time();
$signatureMethod = "HMAC-SHA1";
$version = "1.0";
// Gather all parameters that need to be included in the Authorization header and are know yet
$params = array(
'realm' => $url,
'oauth_consumer_key' => $appToken,
'oauth_token' => $accessToken,
'oauth_nonce' => $nonce,
'oauth_timestamp' => $timestamp,
'oauth_signature_method' => $signatureMethod,
'oauth_version' => $version,
);
$baseString = strtoupper($method) . "&";
$baseString .= rawurlencode($url) . "&";
// Gather, encode, and sort the base string parameters
$encodedParams = array();
foreach ($params as $key => $value){
if ("realm" != $key) {
$encodedParams[rawurlencode($key)] = rawurlencode($value);
}
}
ksort($encodedParams);
//Expand the base string by the encoded parameter=value pairs
$values = array();
foreach ($encodedParams as $key => $value){
$values[] = $key . "=" . $value;
}
$paramsString = rawurlencode(implode("&", $values));
$baseString .= $paramsString;
//Create the signingKey
$signatureKey = rawurlencode($appSecret) . "&" . rawurlencode($accessSecret);
// Create the OAuth signature - Attention: Make sure to provide the binary data to the Base64 encoder
$rawSignature = hash_hmac("sha1", $baseString, $signatureKey, true);
$oAuthSignature = base64_encode($rawSignature);
// Include the OAuth signature parameter in the header parameters array
$params['oauth_signature'] = $oAuthSignature;
// Construct the header string
$header = "Authorization: OAuth ";
$headerParams = array();
foreach ($params as $key => $value){
$headerParams[] = $key . "=\"" . $value . "\"";
}
$header .= implode(", ", $headerParams);
// Get the cURL handler from the library function
$curlHandle = curl_init();
// Set the required cURL options to successfully fire a request to MKM's API
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlHandle, CURLOPT_URL, $url);
curl_setopt($curlHandle, CURLOPT_HTTPHEADER, array($header));
curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, false);
// Execute the request, retrieve information about the request and response, and close the connection
$content = curl_exec($curlHandle);
$info = curl_getinfo($curlHandle);
//foreach ($info as $key => $value){echo $key.": ".$value."\n";}
curl_close($curlHandle);
// Convert the response string into an object
// If you have chosen XML as response format (which is standard) use simplexml_load_string
// If you have chosen JSON as response format use json_decode
//echo $content;
$decoded = json_decode($content);
return $decoded;
}