Search code examples
phpmediawikiwikimedia

How to get the first image of any wiki page


I need to get the first image/ main image in any given wiki page. I could use a scraping tool to do this. But I am using curl to scrap a page. But may be due to slow internet connection, it is taking a long time to scrap just one wiki page. Apart from that I need to display at least 7-8 different wiki images at the same time depending on user's query.

So no point in using curl for this. I tried wiki api

https://en.wikipedia.org/w/api.php?action=query&titles=India&prop=images&imlimit=1

But there are no other parameters that I can give to sort this list. Usually the first image this api is returning is not the main image which you see at the top of the page. Sometimes the image is too far from the context of the page.

I need to display just one image for each wiki title. Thanks in advance.


Solution

  • Seems like the images are getting returned in alphabetical order.... weird.

    Anyway, this might work better:

    https://en.wikipedia.org/w/api.php?action=parse&text={{Barack_Obama}}&prop=images

    Unfortunately, only the first image is usable, but at least it's the right one.