Aug 29 2010

PHP bot to get wikipedia definitions

Category: Php,Spiders & web botsGiulio Pons @ 3:14 pm

Wikipedia, the collaborative and multilingual encyclopedia project, has a lot of usefull terms defined in its database, you can find informations on artists, cities, medical terms, cars, brands… quite everything.
If you need to add some content to your pages without having that content in your database you can use Wikipedia API or Google define query (probably there’s also a Google 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.

The php job is simple: we use CURL to call the API that returns an XML response, we parse it and the get the defnition.
Here is the code that make it for italian wikipedia, you can modify the url to match your wikipedia country site:

function wikidefinition($s) {
	$url = "http://it.wikipedia.org/w/api.php?action=opensearch&search=".urlencode($s)."&format=xml&limit=1";
	$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 "";
	}
}

This code will be added to the MINI BOTS CLASS.

Share

Related posts:

  1. get MySpace events with a PHP function
  2. Tiny url encode and decode with PHP
  3. PHP to get twitter infos and avatar
  4. Parsing Flickr Feed with PHP tutorial
  5. PHP curl bot to update Facebook status

Tags: , , , , , ,

5 Responses to “PHP bot to get wikipedia definitions”

  1. Seyfullah says:

    Good idea, thanks man ;)

  2. Adam Zwakenberg says:

    Hey there,

    I’ve made a modified version of this code that can get multiple results back. You can find it here http://adamzwakk.com/?p=383 :)

  3. Zahid Rouf says:

    hey. Nice post. Thanks for sharing…

  4. WarGot says:

    Good script. Thx man.

  5. jQuery: ottenere una definizione da Wikipedia | Gabriele Romanato says:

    [...] useremo uno script PHP la cui funzione principale è stata adattata a partire da quella proposta in questo articolo. Lo script restituirà un oggetto JSON che verrà poi utilizzato da jQuery all’atto del [...]

Leave a Reply