获取维基百科定义的PHP bot

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了获取维基百科定义的PHP bot相关的知识,希望对你有一定的参考价值。

If you need to add some content to your pages without having that content in your database you can use Wikipedia 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.
  1. function wikidefinition($s) {
  2. $url = "http://it.wikipedia.org/w/api.php?action=opensearch&search=".urlencode($s)."&format=xml&limit=1";
  3. $ch = curl_init($url);
  4. curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
  5. curl_setopt($ch, CURLOPT_POST, FALSE);
  6. curl_setopt($ch, CURLOPT_HEADER, false);
  7. curl_setopt($ch, CURLOPT_NOBODY, FALSE);
  8. curl_setopt($ch, CURLOPT_VERBOSE, FALSE);
  9. curl_setopt($ch, CURLOPT_REFERER, "");
  10. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
  11. curl_setopt($ch, CURLOPT_MAXREDIRS, 4);
  12. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  13. 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");
  14. $page = curl_exec($ch);
  15. $xml = simplexml_load_string($page);
  16. if((string)$xml->Section->Item->Description) {
  17. return array((string)$xml->Section->Item->Text, (string)$xml->Section->Item->Description, (string)$xml->Section->Item->Url);
  18. } else {
  19. return "";
  20. }
  21. }

以上是关于获取维基百科定义的PHP bot的主要内容,如果未能解决你的问题,请参考以下文章

XOOPS - 模块 - MediaWiki维基百科

如何获得Python中的维基百科讨论页内容

PHP 我觉得维基百科

如何从维基百科页面中提取数据

wikipedia 维基百科 语料 获取 与 提取 处理 by python3.5

如何从维基百科中获取所有标题的JSON [关闭]