Bring MediaWiki info back as array


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

Brings single or multiple results from a mediawiki search back as an easy to use array


Copy this code and paste it in your HTML
  1. function curlStart($url){
  2. $ch = curl_init($url);
  3. curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
  4. curl_setopt($ch, CURLOPT_POST, FALSE);
  5. curl_setopt($ch, CURLOPT_HEADER, false);
  6. curl_setopt($ch, CURLOPT_NOBODY, FALSE);
  7. curl_setopt($ch, CURLOPT_VERBOSE, FALSE);
  8. curl_setopt($ch, CURLOPT_REFERER, "");
  9. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
  10. curl_setopt($ch, CURLOPT_MAXREDIRS, 4);
  11. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  12. curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; he; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8");
  13. $page = curl_exec($ch);
  14. return $page;
  15. }
  16.  
  17. function findWikiInfo($s) {
  18. $page = curlStart("http://en.wikipedia.org/w/api.php?action=opensearch&search=".urlencode($s)."&format=xml&limit=10");
  19. $xmlpage = simplexml_load_string($page);
  20. $wikiarray = array();
  21. if(count($xmlpage->Section->Item) == 1){
  22. array_push($wikiarray, array((string)$xmlpage->Section->Item->Text, (string)$xmlpage->Section->Item->Description, (string)$xmlpage->Section->Item->Url));
  23. return $wikiarray;
  24. } else {
  25. for($i = 0;$i != count($xmlpage->Section->Item); $i++){
  26. array_push($wikiarray, array((string)$xmlpage->Section->Item[$i]->Text, (string)$xmlpage->Section->Item[$i]->Description, (string)$xmlpage->Section->Item[$i]->Url));
  27. }
  28. return $wikiarray;
  29. }
  30. }
  31.  
  32. print_r(findWikiInfo($_GET['search']));

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.