How To Create Virtual Wikipedia
In this tutorial learn how to build a virtual Wikipedia using PHP. Wikipedia is very popular and most powerful online encyclopedia, By implementing virtual Wikipedia you can let users to read Wikipedia articles in your website.
The Wikipedia API
Wikipedia uses MediaWiki CMS, Using MediaWiki API we can implement Virtual Wikipedia.
The XML URL
http://en.wikipedia.org/w/api.php?action=parse&page=india&prop=text&format=xml
The XML Response Contains html code of Wikipedia Page India.
wiki.php
<?php
$var=@$_GET['title'];
if($var)
{
ini_set( 'user_agent', 'neinsun' );
$file = "http://en.wikipedia.org/w/api.php?action=parse&page=$var&prop=text&format=xml";
function contents($parser, $data){
$new=str_replace("http://en.wikipedia.org/wiki","/wiki",$data);
print $new;
}
function startTag($parser, $data){
echo " ";
}
function endTag($parser, $data){
echo " <br />";
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startTag", "endTag");
xml_set_character_data_handler($xml_parser, "contents");
$fp = fopen($file, "r");
$data = fread($fp, 2000);
ini_set( 'user_agent', 'Nannu' );
$homepage = file_get_contents($file);
if(!(xml_parse($xml_parser, $homepage, 800000))){
die("Error on line " . xml_get_current_line_number($xml_parser));
}
xml_parser_free($xml_parser);
fclose($fp);
}
else
{
echo"Error 404";
}
?>
Wikipedia does not allow php client directly without ‘ini_set’.
Now very important task .htaccess
RewriteEngine On RewriteRule ^wiki/(.+)$ /wiki.php?title=$1 [PT,L,QSA]
The above htaccess rule makes http://www.example.com/wiki/India to http://www.example.com/wiki.php?title=India. With this you can read any wikipedia article just like http://www.example.com/wiki/
You Might Also Like
Filed Under: apps, PHP, Technology