wikipedia.php 843 B

12345678910111213141516171819202122232425262728
  1. <?php
  2. function wikipedia_results($query, $response)
  3. {
  4. $query_encoded = urlencode($query);
  5. $json_response = json_decode($response, true);
  6. $first_page = array_values($json_response["query"]["pages"])[0];
  7. if (!array_key_exists("missing", $first_page))
  8. {
  9. $description = substr($first_page["extract"], 0, 250) . "...";
  10. $source = "https://en.wikipedia.org/wiki/$query";
  11. $response = array(
  12. "special_response" => array(
  13. "response" => $description,
  14. "source" => $source
  15. )
  16. );
  17. if (array_key_exists("thumbnail", $first_page))
  18. $response["special_response"]["image"] = $first_page["thumbnail"]["source"];
  19. return $response;
  20. }
  21. }
  22. ?>