Saturday, December 15, 2012

Creating a PHP application on top of Wikipedia using the Wikipedia API

API or Application programming interface is a set of functions which you can use to perform various tasks.Wikipedia has its own API to use the wiki-data  in our web applications(to know more about this API click here). Using this API we can retrieve any information in the form of JSON and XML. XML and JSON are just two different ways to represent data.I will deal with XML and JSON in detail later.Now to understand how this API is going to work we are going for a sample application.we will do a open-search and return the top 5 Wikipedia search results using the Wikipedia API.
  1.  Open a text editor and save it as index.php 
  2.  In PHP we are going to use cURL concept. cURL is a way you can hit a URL from your code to get a html response from it. cURL means client URL which allows you to connect with other URLs and use their responses in your code.
  3. Now using this curl  we are going to send a request to wikipedia api through a URL and get back the wiki-data as response in the format of JSON or XML.
  4. The API URL for open-search looks something like this. "http://en.wikipedia.org/w/api.php?format=xml&action=opensearch&search=YOUR_QUERY&limit=5"
  5. Here action refers to open search i.e you are doing a open search on the given search query and need the top 5 search results.
  6. Now your app is almost complete. Open-search is just one feature of Wikipedia API.You can do much more and can refer to this.
  7. To make it more dynamic. Ask the user for a query and pass it to your php application using $_GET and replace it instead of YOUR_QUERY.
  8. You can download the source code here.If you want a detailed explanation of code i would love to go  so but first i want you to browse through code and try deploying one on your own.
  9. If you want a live demo try this "http://wikisrch.herokuapp.com?message=google".I deployed the above app in heroku cloud for my 55444 app and it works for different parameters. Try changing Google to something else and try again.
  10. Similarly different websites have different API and i would be covering a few in the upcoming tutorials so stay tuned and comment for clarifications and further assistance.


2 comments:

  1. Thank you very much for sharing this informative and wonderful post. Before I was not sure about things but after read this article I think we can create more attractive PHP Apps using Wikipedia API.
    Hire PHP Developer India for Best PHP Development Services

    ReplyDelete
    Replies
    1. Thank you sir..As a PHP lover this has been a great encouragement

      Delete