· 9 years ago · Nov 15, 2016, 01:24 AM
1<?php
2 $set = set_time_limit(0);
3 //$rehab = 2533274795705427; // xUID for 'xX rehab Xx' | may not even need this anymore
4
5 // function to set standard parameters of API connection
6 function api_set_conn(&$ch) {
7
8 $authKey = "a178e5d5479f1e80e2980e7a451652e435dc40e1";
9 $xAuth = array("X-AUTH: $authKey");
10
11 // set URL and other appropriate options
12 curl_setopt($ch, CURLOPT_HTTPHEADER, $xAuth); // insert the authentication token into the header
13 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return variable
14 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow redirects
15 curl_setopt($ch, CURLOPT_AUTOREFERER, true); // set referrer on redirect
16 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // do not verify SSL cert
17 curl_setopt($ch, CURLINFO_HEADER_OUT, true); // track the handle's request string
18 }
19
20
21 function api_fetch_lib($gt, $plat) {
22
23 //@@@@@@@@@@@
24 // Pretty sure we can remove this and the !isset() since we validate before ever calling api_fetch();
25 // make sure we have a valid id before making expensive API calls
26 $xUID = validate_user($gt);
27
28
29 // if it was an invalid ID, end the function and return an empty array which will be ignored by functions using it
30 if(!isset($xUID) || $xUID == 0)
31 { return array(); }
32
33
34 // establish a new API connection
35 $ch = curl_init();
36 api_set_conn($ch);
37
38
39 // prepare the API endpoints for specified user
40 $url_X360 = "https://xboxapi.com/v2/" . $xUID . "/xbox360games";
41 $url_XBONE = "https://xboxapi.com/v2/" . $xUID . "/xboxonegames";
42 $url_lib;
43
44
45 if ($plat == 'X360')
46 $url_lib = $url_X360;
47 else
48 $url_lib = $url_XBONE;
49
50
51 // set endpoint for API request
52 curl_setopt($ch, CURLOPT_URL, $url_lib);
53
54
55 // execute API call and convert the JSON string returned by the API into a PHP variable
56 // $titles is the sub-array of the returned API string
57 $data = json_decode(curl_exec($ch), true);
58
59 // close cURL resource, and free up system resources
60 curl_close($ch);
61
62 return $data['titles'];
63 }
64
65
66 // loop through the returned JSON string and print out the name of each game in the library
67 //echo "<h2>XBOX 360</h2></br>";
68 foreach ($titles as $t) {
69 //echo "Title: " . $t['name'] . "</br>";
70 get_info($ch, $t, 'X-Box 360');
71 echo "</br>";
72 }
73
74 function validate_user($gt) {
75
76 // establish a new API connection
77 $ch = curl_init();
78 api_set_conn($ch);
79
80 $url_xuid = "https://xboxapi.com/v2/xuid/$gt"; // API endpoint to convert gamertag -> xUID
81 curl_setopt($ch, CURLOPT_URL, $url_xuid); // set the API endpoint
82
83 // return the xUID for the enter gamertag
84 return curl_exec($ch);
85 }
86
87
88 function check_user($gt) {
89 //CHECK USER TABLE FOR USER
90 $servername = "localhost";
91 $username = "root";
92 $password = "password";
93 $dbname = "gamessite";
94
95 // Create connection
96 $conn = new mysqli($servername, $username, $password, $dbname);
97
98 // Check connection
99 if ($conn->connect_error) {
100 die("Connection failed: " . $conn->connect_error);
101 }
102 //echo "Connected successfully";
103
104 // I read this as de-mousers more times than I care to admit
105 //check is user already exists in user table
106 $sql = "INSERT IGNORE INTO demousers
107 VALUES
108 ('".$gt."')";
109
110 //check if insert was successful
111 if ($conn->query($sql) === TRUE)
112 {
113 //echo "New record created successfully";
114 }
115 else
116 {
117 echo "Error: " . $sql . "<br>" . $conn->error;
118 }
119
120
121 // @@@@@@@@@
122 // $lib will be an array of a user's X360 games; it is equivalent to the old $titles
123 // probably should turn everything from ln126 to ln142 into a function for reuse
124 $uPlat = 'X360';
125 $lib = api_fetch_lib($gt, $uPlat);
126 foreach ($lib as $uTitle) {
127 // Push user's games to demolibrary
128 $uGID = $sql = "SELECT GID
129 FROM games
130 WHERE Title = '".$uTitle."' AND Platform = '".$uPlat."';"
131
132 $sql = "INSERT IGNORE INTO demolibrary
133 VALUES
134 ('".$gt."', '".$uGID."')";
135
136 //check if insert was successful
137 if ($conn->query($sql) === TRUE)
138 {
139 //echo "New record created successfully";
140 }
141 else
142 {
143 echo "Error: " . $sql . "<br>" . $conn->error;
144 }
145 }
146
147
148 // @@@@@@@@@
149 // $lib will be an array of a user's XBONE games; it is equivalent to the old $titles
150 $uPlat = 'XONE';
151 $lib = api_fetch_lib($gt, $uPlat);
152 foreach ($lib as $uTitle) {
153 // Push user's games to demolibrary
154 $uGID = $sql = "SELECT GID
155 FROM games
156 WHERE Title = '".$uTitle."' AND Platform = '".$uPlat."';"
157
158 $sql = "INSERT IGNORE INTO demolibrary
159 VALUES
160 ('".$gt."', '".$uGID."')";
161
162 //check if insert was successful
163 if ($conn->query($sql) === TRUE)
164 {
165 //echo "New record created successfully";
166 }
167 else
168 {
169 echo "Error: " . $sql . "<br>" . $conn->error;
170 }
171 }
172
173
174
175 //*****end of loop
176
177
178 //close connection
179 $conn->close();
180 }
181
182
183 function DB_intersection_print($gt1 , $gt2)
184 {
185 $servername = "localhost";
186 $username = "root";
187 $password = "password";
188 $dbname = "gamessite";
189
190 // Create connection
191 $conn = new mysqli($servername, $username, $password, $dbname);
192
193 // Check connection
194 if ($conn->connect_error) {
195 die("Connection failed: " . $conn->connect_error);
196 }
197 //echo "Connected successfully";
198
199 //sql query call for the intersection
200 $sql = "SELECT *
201 FROM games
202 WHERE GID = ((SELECT GID
203 FROM demolibrary
204 WHERE gamertag = '".$gt1."')
205 INTERSECT
206 (SELECT GID
207 FROM demolibrary
208 WHERE gamertag = '".$gt2"')
209 )";
210
211 //close connection
212 $conn->close();
213 }
214
215
216
217
218 /*
219 THIS IS THE SCRIPT TO PULL THE GAME INFO FOR A SPECIFIED GAME
220 STILL NEED TO TURN THIS INTO A LOOP TO ITERATE THROUGH ALL GAMES IN A LIBRARY
221 DO NOT UNCOMMENT AND EXPECT IT TO FULLY FUNCTION
222 */
223 //@@@@@@@@@@@@@@@@@@
224 // NOTE: AS OF NOW THIS FUNCTION IS NOT USED IN ANY OTHER FUNCTION!
225 function get_info(&$ch, $titles, $platform) {
226
227 $url_gameinfo = "https://xboxapi.com/v2/game-details-hex/"; // API endpoint to pull game info; append dechex(#titleId#)
228 $check = $titles['titleId'];
229 $check = dechex($check);
230
231
232 curl_setopt($ch, CURLOPT_URL, $url_gameinfo . $check);
233 $gameinfo = json_decode(curl_exec($ch), true);
234
235 $game_item = $gameinfo['Items'][0];
236 //$game_img = $game_item['Images'];
237
238 $info = array (try_info($game_item['Name']),
239 try_info($game_item['Description']),
240 /*try_info($game_item['ReleaseDate']),*/
241 try_info($game_item['DeveloperName']),
242 try_info($game_item['PublisherName']),
243 /*try_info($game_item['AllTimeAverageRating'])*/
244 );
245
246
247 $servername = "localhost";
248 $username = "root";
249 $password = "password";
250 $dbname = "gamessite";
251
252 // Create connection
253 $conn = new mysqli($servername, $username, $password, $dbname);
254
255 // Check connection
256 if ($conn->connect_error) {
257 die("Connection failed: " . $conn->connect_error);
258 }
259 //echo "Connected successfully";
260
261
262 //create escape strings for use of " ' " in the sql query
263 $title_pass = $info[0];
264 $title_pass = $conn->real_escape_string($title_pass);
265 $pub_pass = $info[3];
266 $pub_pass = $conn->real_escape_string($pub_pass);
267 $dev_pass = $info[2];
268 $dev_pass = $conn->real_escape_string($dev_pass);
269
270
271 $sql = "INSERT IGNORE INTO games
272 VALUES
273 (0, '".$title_pass."' , '".$platform."', '".$pub_pass."', '".$dev_pass."')";
274
275
276 //check if insert was successful
277 if ($conn->query($sql) === TRUE) {
278 //echo "New record created successfully";
279 } else {
280 echo "Error: " . $sql . "<br>" . $conn->error;
281 }
282
283 //close connection
284 $conn->close();
285
286
287 //print_r($info);
288
289 //echo "</br>Banner IMG: <img src='" . $game_img[0]['Url'] . "'></br>";
290 //echo "</br>BoxArt IMG: <img src='" . $game_img[1]['Url'] . "'></br>";
291
292 }
293
294 function info_failed($errno, $errstr) {
295 return "N/A";
296 }
297
298 // error handler if the API can't return the proper info
299 function try_info($get) {
300 return $get;
301 }
302
303
304
305?>