Question

I am very entry level php. I am fairly proficient in wordpress and I have created some simple plugins. My situation is, I would like to recreate google's way of displaying a wikipedia article. I would like to be able to use a shortcode with the person's name and have it return the same results google would return and styled the same way just on my wordpress website. I know Wikipedia has an api that allows for search and display, I am just trying to wrap my head around the process. If someone could point me in the right direction of how this would be achieved in php or wordpress I would really appreciate it. I know there are some similar questions on here about the wiki api but I would like to hear some different approaches to finding the best way to achieve what google is doing.

If you don't know what I am talking about, try googling someone famous who would have a wiki article and it will display on the right hand side of the screen with their photo and their info in a very nicely displayed box. Can this overall page info be queried all at once, or does each piece of information have to be queried from wiki and then displayed that way with css?

Forgive me if this is to vague or has already been covered. I am very interested in peoples logic to approaching this situation. Any information on this would be very helpful.

Was it helpful?

Solution

You'll be lucky if this doesn't get downvoted to hell, but I'll try and give you at least a basic run-through.

There are two ways to approach this. AJAX or PHP. The PHP method is a little more complicated to wrap your head around (at least for me anyway).

PHP

First, you should sit down and REALLY read the Wikipedia API manual and sources. The people behind Wikipedia have put a ton of effort into it and wouldn't have done so without giving you information on how to use their system. Don't be intimidated--it's really not that hard.

Second, after you've read the API, you'll know what this url means.

http://en.wikipedia.org/w/api.php?format=json&action=query&titles=Main%20Page&prop=revisions&rvprop=content

A method that might work without using CURL, which can be very confusing, is file_get_contents().

So set the query string parameters for the api, and use them like so:

$api_call = file_get_contents('http://en.wikipedia.org/w/api.php?format=json&action=query&titles=Main%20Page&prop=revisions&rvprop=content');
$api_data = json_decode( $homepage );

Now you should have an array that you should easily be able to manipulate and place into your site.


jQuery/AJAX

Way easier in my opinion, and you have the added bonus of some user manipulation:

$.ajax({
  type: 'GET',
  url: 'http://en.wikipedia.org/w/api.php?format=json&action=query&titles=Main%20Page&prop=revisions&rvprop=content'
}).done(function(data){
    var d = $.parseJSON( data );
    $('div.data-holder').html( 'foo'+d.bar );
});

None of this is tested. Just meant for a general idea. Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top