search.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php require_once "../utilities/index.php" ?>
  2. <?php
  3. $title = htmlspecialchars($_GET["query"]);
  4. ?>
  5. <?php require_once "../elements/header.php" ?>
  6. <?php require_once "../elements/item.php" ?>
  7. <?php
  8. if (isset($_GET["query"]))
  9. echo "<h1>Search: “" . htmlspecialchars($_GET["query"]) . "”</h1>";
  10. $ch = curl_init("https://bandcamp.com/api/bcsearch_public_api/1/autocomplete_elastic");
  11. curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type: application/json"]);
  12. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
  13. "search_text" => $_GET["query"],
  14. "search_filter" => "",
  15. "full_page" => true
  16. ]));
  17. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  18. $results = json_decode(curl_exec($ch))->auto->results;
  19. if (empty($results))
  20. echo_error_message();
  21. echo "<div class=\"results\">";
  22. foreach ($results as $result) {
  23. $link = $result->item_url_path ?? $result->item_url_root;
  24. $link = convert_bandcamp_link($link);
  25. unset($text);
  26. switch ($result->type) {
  27. case "a":
  28. $text = "by " . htmlspecialchars($result->band_name);
  29. break;
  30. case "t":
  31. $text = "by " . htmlspecialchars($result->band_name);
  32. $text .= "<br>";
  33. $text .= "on " . htmlspecialchars($result->album_name ?? $result->name);
  34. break;
  35. };
  36. echo_item($link, convert_bandcamp_link(resize_link($result->img, 3)), htmlspecialchars($result->name), $text ?? null);
  37. };
  38. echo "</div>";
  39. ?>
  40. <?php require_once "../elements/footer.php" ?>