api.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. $config = require "config.php";
  3. require "misc/tools.php";
  4. if (!isset($_REQUEST["q"]))
  5. {
  6. echo "<p>Example API request: <a href=\"./api.php?q=gentoo&p=2&t=0\">./api.php?q=gentoo&p=2&t=0</a></p>
  7. <br/>
  8. <p>\"q\" is the keyword</p>
  9. <p>\"p\" is the result page (the first page is 0)</p>
  10. <p>\"t\" is the search type (0=text, 1=image, 2=video, 3=torrent, 4=tor)</p>
  11. <br/>
  12. <p>The results are going to be in JSON format.</p>
  13. <p>The API supports both POST and GET requests.</p>";
  14. die();
  15. }
  16. $query = $_REQUEST["q"];
  17. $query_encoded = urlencode($query);
  18. $page = isset($_REQUEST["p"]) ? (int) $_REQUEST["p"] : 0;
  19. $type = isset($_REQUEST["t"]) ? (int) $_REQUEST["t"] : 0;
  20. $results = array();
  21. switch ($type)
  22. {
  23. case 0:
  24. require "engines/google/text.php";
  25. $results = get_text_results($query, $page);
  26. break;
  27. case 1:
  28. require "engines/qwant/image.php";
  29. $results = get_image_results($query_encoded, $page);
  30. break;
  31. case 2:
  32. require "engines/invidious/video.php";
  33. $results = get_video_results($query_encoded);
  34. break;
  35. case 3:
  36. if ($config->disable_bittorent_search)
  37. $results = array("error" => "disabled");
  38. else
  39. {
  40. require "engines/bittorrent/merge.php";
  41. $results = get_merged_torrent_results($query_encoded);
  42. }
  43. break;
  44. case 4:
  45. if ($config->disable_hidden_service_search)
  46. $results = array("error" => "disabled");
  47. else
  48. {
  49. require "engines/ahmia/hidden_service.php";
  50. $results = get_hidden_service_results($query_encoded);
  51. }
  52. break;
  53. default:
  54. require "engines/google/text.php";
  55. $results = get_text_results($query_encoded, $page);
  56. break;
  57. }
  58. header("Content-Type: application/json");
  59. echo json_encode($results);
  60. ?>