No Description

listHistory.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. require_once('config.php');
  3. // Filter unsupported HTTP requests
  4. if ($_SERVER["REQUEST_METHOD"] !== "GET") {
  5. http_response_code(400); // use appropriate status
  6. echo json_encode(array("error" => "Endpoint only supports GET requests"));
  7. exit();
  8. }
  9. // Get user input
  10. $userID = isset($_GET['user_id']) ? $_GET['user_id'] : '';
  11. // Clean user input
  12. $userID = $connection->real_escape_string($userID);
  13. function fetchSingleFlower($flowerID) {
  14. global $TREFFLE_BASE_URL, $TREFFLE_LIST_PATH, $TREFFLE_TOKEN;
  15. $endpoint = $TREFFLE_BASE_URL . $TREFFLE_LIST_PATH . "/$flowerID";
  16. $queryParams = array("token" => $TREFFLE_TOKEN);
  17. $curl = curl_init();
  18. $url = sprintf("%s?%s", $endpoint, http_build_query($queryParams));
  19. curl_setopt($curl, CURLOPT_URL, $url);
  20. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  21. $response = curl_exec($curl);
  22. $http_response_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  23. curl_close($curl);
  24. return array("data" => json_decode($response), "code" => $http_response_code);
  25. }
  26. function listHistory($userID) {
  27. if ($userID == '') {
  28. http_response_code(400); // use appropriate status code
  29. echo json_encode(array("error" => "Field 'user_id' is required"));
  30. return;
  31. }
  32. global $connection;
  33. $query = "SELECT *
  34. FROM UserHistory AS H
  35. WHERE H.user_id = '$userID';";
  36. if ($result = $connection->query($query)) {
  37. // Create flower container
  38. $flowers = [];
  39. // Loop through user's flowers
  40. while($row = $result->fetch_assoc()) {
  41. // Query API
  42. $apiOutput = fetchSingleFlower($row['flower_id']);
  43. $data = $apiOutput['data'];
  44. $code = $apiOutput['code'];
  45. // Handle API errors
  46. if ($code >= 300 || $code < 200) {
  47. http_response_code($code);
  48. echo json_encode(array("error" => "Unknown error occurred with Treffle API (Code: $code)"));
  49. return;
  50. } elseif (isset($data->error) && $data->error == "true") {
  51. http_response_code(400);
  52. $msg = isset($data->message) ? $data->message : $data->messages;
  53. echo json_encode(array("error" => $msg));
  54. return;
  55. }
  56. // Extract flower
  57. $flower = $data->data;
  58. $flower->isFavorite = $row['in_wishlist'] == "1" ? true : false;
  59. $flower->hasBeenFound = $row['has_been_found'] == "1" ? true : false;
  60. // Append flower
  61. $flowers[] = $flower;
  62. }
  63. // Return response
  64. http_response_code(200);
  65. $response = new stdClass;
  66. $response->data = $flowers;
  67. echo json_encode($response);
  68. } else {
  69. http_response_code(500); // use appropriate status code
  70. echo json_encode(array("error" => $connection->error));
  71. }
  72. }
  73. listHistory($userID);