PHP bot to get wikipedia definitions

/ Published in: PHP
Save to your folder(s)

If you need to add some content to your pages without having that content in your database you can use Wikipedia API. You can, for example, need to add automatically a simple description to a city name, or to a band name. Or you could need to add the definition of some technological terms. You can do all of this things using Wikipedia, since Wikipedia has an API that easily lets you do it.

Copy this code and paste it in your HTML
  1. function wikidefinition($s) {
  2. $url = "".urlencode($s)."&format=xml&limit=1";
  3. $ch = curl_init($url);
  4. curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
  5. curl_setopt($ch, CURLOPT_POST, FALSE);
  6. curl_setopt($ch, CURLOPT_HEADER, false);
  7. curl_setopt($ch, CURLOPT_NOBODY, FALSE);
  8. curl_setopt($ch, CURLOPT_VERBOSE, FALSE);
  9. curl_setopt($ch, CURLOPT_REFERER, "");
  10. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
  11. curl_setopt($ch, CURLOPT_MAXREDIRS, 4);
  12. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  13. curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; he; rv: Gecko/20100722 Firefox/3.6.8");
  14. $page = curl_exec($ch);
  15. $xml = simplexml_load_string($page);
  16. if((string)$xml->Section->Item->Description) {
  17. return array((string)$xml->Section->Item->Text, (string)$xml->Section->Item->Description, (string)$xml->Section->Item->Url);
  18. } else {
  19. return "";
  20. }
  21. }


Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.