We connect Wikipedia to the site
Now almost every site has buttons to go to the twitter site or bookmark the page. But for some reason, you can see very few Wikipedia icons near the names of great people or terms.
It may seem to someone superfluous to load the site with such functionality, but it is better to ask users about it. So, in one new project, it was proposed to give links to Wikipedia for authors of books.
If you consider that only active users will click on the “Leave your feedback” button and vote, then probably 33 votes can be multiplied by 10 - 100.
Having figured out the api Wikipedia, I would like to offer a solution to automate adding links to Wikipedia as little as possible.
Knowing them, the surname or patronymic of a famous person, it is impossible to be 100% sure that this is exactly the person we need. For example, if we need to give a link to Alexander Ivanov, Ivanov, on Wikipedia there are a couple of people with the same data and can’t automatically determine who suits us. The same situation with the names of books and films. For example, we need an article about a book; on Wikipedia, there may be an article about a film. My solution is to do a search query using the wiki and already choose which is more suitable.
Wikipedia has a very powerful api www.mediawiki.org/wiki/API/en
All parameters are described here en.wikipedia.org/w/api.php (eng)
Of the many parameters we need
Action - the action we want to perform. We need a search for Action = opensearch
Search - what are we looking for. For example, “Master and Margarita”
search =% EC% E0% F1% F2% E5% F0% 20% E8% 20% EC% E0% F0% E3% E0% F0% E8% F2% E0
Prop - what characteristics of the page do we want receive. We need general information about the page: title, description
prop = info
Format - the format in which the result is returned. To search, use xml
format = xml
inprop- what additional information we want to receive. We still need a link to the Wikipedia page, so
inprop = url The
entire line with all the parameters will look like
ru.wikipedia.org/w/api.php?action=opensearch&search=%EC%E0%F1%F2%E5%F0%20 % E8% 20% EC% E0% F0% E3% E0% F0% E8% F2% E0 & prop = info & format = xml & inprop = url
example function in php to get page data
Completely example and script .
Maybe the time has come when there is a link to Wikipedia next to every famous name or term?
It may seem to someone superfluous to load the site with such functionality, but it is better to ask users about it. So, in one new project, it was proposed to give links to Wikipedia for authors of books.
If you consider that only active users will click on the “Leave your feedback” button and vote, then probably 33 votes can be multiplied by 10 - 100.
Having figured out the api Wikipedia, I would like to offer a solution to automate adding links to Wikipedia as little as possible.
Difficulties
Knowing them, the surname or patronymic of a famous person, it is impossible to be 100% sure that this is exactly the person we need. For example, if we need to give a link to Alexander Ivanov, Ivanov, on Wikipedia there are a couple of people with the same data and can’t automatically determine who suits us. The same situation with the names of books and films. For example, we need an article about a book; on Wikipedia, there may be an article about a film. My solution is to do a search query using the wiki and already choose which is more suitable.
Api wiki
Wikipedia has a very powerful api www.mediawiki.org/wiki/API/en
All parameters are described here en.wikipedia.org/w/api.php (eng)
Of the many parameters we need
Action - the action we want to perform. We need a search for Action = opensearch
Search - what are we looking for. For example, “Master and Margarita”
search =% EC% E0% F1% F2% E5% F0% 20% E8% 20% EC% E0% F0% E3% E0% F0% E8% F2% E0
Prop - what characteristics of the page do we want receive. We need general information about the page: title, description
prop = info
Format - the format in which the result is returned. To search, use xml
format = xml
inprop- what additional information we want to receive. We still need a link to the Wikipedia page, so
inprop = url The
entire line with all the parameters will look like
ru.wikipedia.org/w/api.php?action=opensearch&search=%EC%E0%F1%F2%E5%F0%20 % E8% 20% EC% E0% F0% E3% E0% F0% E8% F2% E0 & prop = info & format = xml & inprop = url
example function in php to get page data
function get_wiki_url($title)
{
//устанавливаем соединение через сокет
$fp = fsockopen("ru.wikipedia.org", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)
\n";
} else {
$out = "GET /w/api.php?action=opensearch&search=".urlencode($title)."& prop=info&format=xml&inprop=url HTTP/1.1\r\n";
$out .= "Host: ru.wikipedia.org\r\n";
// указывает User-Agent. Без него будет ошибка
$out .= "User-Agent: MyCuteBot/0.1\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
$str = '';
// получает только xml без полученных заголовков сервера
while (!feof($fp)) {
$tmp_str = fgets($fp, 128);
if ($str != '' || substr($tmp_str,0,2)=='
$str .= $tmp_str;
}
fclose($fp);
//парсим строку
$xml = simplexml_load_string($str);
return $xml->Section->Item;
}
}
$pages_data = get_wiki_url("Мастер и Маргарита");
?>
* This source code was highlighted with Source Code Highlighter.
Completely example and script .
Maybe the time has come when there is a link to Wikipedia next to every famous name or term?