1

I want to take some information about my query from wikipedia using php. I want to have a description, title, img url (if exists), and some side information about it. For example like the google wikipedia information, with much side information.

Until now i have only the description & title.

My code:

        $q = str_replace("#", "", ucwords($query));
        $opts = array('http' =>
          array(
            'user_agent' => 'Mozilla Firefox www (http://www.mozilla.org)'
          )
        );
        $context = stream_context_create($opts);
        $url = 'en.wikipedia.org/w/api.php?action=opensearch&search='.$q.'&limit=1&namespace=0&format=xml';
        @$wiki_arr = simplexml_load_file($url);
        if(@$wiki_arr->Section->Item->Text == "" or @strtolower($wiki_arr->Section->Item->Text) != strtolower($q))
        {
            return "";
        }

        $title = $wiki_arr->Section->Item->Text;
        $description = $wiki_arr->Section->Item->Description;
        $url = $wiki_arr->Section->Item->Url;
        $result = "".$title.", ".$description.", ".$url."";
R2D2
  • 743
  • 1
  • 7
  • 14
  • So what exactly is the question? – Billy Oct 19 '14 at 13:36
  • how can i take the information using the wikipedia api? – R2D2 Oct 19 '14 at 14:01
  • You already know as you're already getting info from it (description, title ). Wikipedia API has a help page here [WIKI API](http://www.mediawiki.org/wiki/API:Main_page) – Billy Oct 19 '14 at 14:05
  • i wouldn't ask the question here if i had found the answer in the help page .. I didn't found the option to get the side information about an wikipedia article – R2D2 Oct 19 '14 at 14:11
  • check this SO question [ SO question wikipedia API just for retrieve content summary](http://stackoverflow.com/questions/8555320/is-there-a-clean-wikipedia-api-just-for-retrieve-content-summary) – Billy Oct 19 '14 at 14:16
  • What exactly do you mean by "side information"? – svick Oct 19 '14 at 15:36
  • I mean for example if the article is about eric clapton that he gives me the birthdate, birthplace, what kind of star he is and other interesting information. – R2D2 Oct 19 '14 at 18:23
  • You will need to parse the infobox to get the "side information" -- get the page using the API, parse it using DOMDocument, and extract the data from the infobox that way. – i alarmed alien Oct 21 '14 at 21:28

1 Answers1

0

Once again, there is no such thing as a Wikipedia API. There is a MediaWiki web API. If something doesn't exist there, it doesn't exist at all in wikipedia.org.

For your question: you said you want to do something like the Wikipedia card in the Google Knowledge Graph. What powers that? Freebase. What's the Wikimedia projects equivalent of Freebase? Wikidata.org. (And not only for Wikipedia, but also for Wikisource etc.) So, please use the Wikidata data, via its API which is documented by itself: http://wikidata.org/w/api.php

You can check the [[wikidata:list of properties]] to know what kind of data to access, and [[mediawikiwiki:Category:Wikibase]] for other libraries to access the API. An example of what you can do this way is wdsearch.js, another reasonator at tools.wmflabs.org (sorry, can't add more links).

Nemo
  • 2,441
  • 2
  • 29
  • 63