PDA

View Full Version : Wikipedia API loop



milos87popovic
04-11-2014, 06:20 PM
Hi guys, I making website who use information from wikipedia.
I use this script:


<?php
//FUNCTION THAT :PARAMETER - KEYWORD , AND RETURNS WIKI DEFINITION (IN ARRAY FORMAT)
function wikidefinition($s) {
//ENGLISH WIKI
$url = "http://en.wikipedia.org/w/api.php?action=opensearch&search=".urlencode($s)."&format=xml&limit=10";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
curl_setopt($ch, CURLOPT_POST, FALSE);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_NOBODY, FALSE);
curl_setopt($ch, CURLOPT_VERBOSE, FALSE);
curl_setopt($ch, CURLOPT_REFERER, "");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_MAXREDIRS, 4);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
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");

$page = curl_exec($ch);
$xml = simplexml_load_string($page);
if((string)$xml->Section->Item->Description) {
return array((string)$xml->Section->Item->Text,
(string)$xml->Section->Item->Description,
(string)$xml->Section->Item->Url);
} else {
return "";
}
}
//END OF FUNCTION WIKIDEFINITIONS



//USE OF FUNCTION
$data = wikidefinition('A CAPPELLA') ;
//var_dump( wikidefinition('bangladesh') ) ; //displays the array content
echo "Word:" . $data[0] . "<br/>";
echo "Definition:" . $data[1] . "<br/>";
echo "Link:" . $data[2] . "<br/>";
?>


This give me only one result from wikipedia. I wont 10 results, I set this on $url.
How can I make a loop to print this results.

Thanks a lot...

arianagrand
04-12-2014, 08:57 AM
Hello ..

Standard WordPress theme files don’t merely contain HTML and content tags, they also contain The Loop. The Loop is the heart of WordPress output, and it is how WordPress acquires, processes, and iterates through a set of posts to convert them from database table rows to e.g. your blog’s home page.

milos87popovic
04-12-2014, 07:52 PM
It's not for WordPress - this is clear php no CMS...