Skip to content

Instantly share code, notes, and snippets.

@xwiz
Forked from iwek/grab-images-wikipedia.php
Created June 12, 2013 17:15
Show Gist options
  • Save xwiz/5767264 to your computer and use it in GitHub Desktop.
Save xwiz/5767264 to your computer and use it in GitHub Desktop.

Revisions

  1. @iwek iwek renamed this gist Jul 17, 2012. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. @iwek iwek revised this gist Jul 12, 2012. 1 changed file with 12 additions and 11 deletions.
    23 changes: 12 additions & 11 deletions grab-images-wikipedia
    Original file line number Diff line number Diff line change
    @@ -26,18 +26,19 @@ function getResults($json){
    $json_array = json_decode($json, true);

    foreach($json_array['query']['pages'] as $page){
    foreach($page['images'] as $image){

    $title = str_replace(" ", "_", $image["title"]);
    $imageinfourl = "http://en.wikipedia.org/w/api.php?action=query&titles=".$title."&prop=imageinfo&iiprop=url&format=json";
    $imageinfo = curl($imageinfourl);
    $iamge_array = json_decode($imageinfo, true);
    $image_pages = $iamge_array["query"]["pages"];

    foreach($image_pages as $a){
    $results[] = $a["imageinfo"][0]["url"];
    if(count($page['images']) > 0){
    foreach($page['images'] as $image){

    $title = str_replace(" ", "_", $image["title"]);
    $imageinfourl = "http://en.wikipedia.org/w/api.php?action=query&titles=".$title."&prop=imageinfo&iiprop=url&format=json";
    $imageinfo = curl($imageinfourl);
    $iamge_array = json_decode($imageinfo, true);
    $image_pages = $iamge_array["query"]["pages"];

    foreach($image_pages as $a){
    $results[] = $a["imageinfo"][0]["url"];
    }
    }

    }
    }

  3. @iwek iwek created this gist Jul 12, 2012.
    70 changes: 70 additions & 0 deletions grab-images-wikipedia
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,70 @@
    <?php

    /**
    * Grab Images from Wikipedia via thier API
    *
    * @author http://techslides.com
    * @link http://techslides.com/grab-wikipedia-pictures-by-api-with-php
    */

    //curl request returns json output via json_decode php function
    function curl($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 GTB5');
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
    }

    //parse the json output
    function getResults($json){

    $results = array();

    $json_array = json_decode($json, true);

    foreach($json_array['query']['pages'] as $page){
    foreach($page['images'] as $image){

    $title = str_replace(" ", "_", $image["title"]);
    $imageinfourl = "http://en.wikipedia.org/w/api.php?action=query&titles=".$title."&prop=imageinfo&iiprop=url&format=json";
    $imageinfo = curl($imageinfourl);
    $iamge_array = json_decode($imageinfo, true);
    $image_pages = $iamge_array["query"]["pages"];

    foreach($image_pages as $a){
    $results[] = $a["imageinfo"][0]["url"];
    }

    }
    }

    return $results;

    }

    $search = $_GET["q"];

    if (empty($search)) {
    //term param not passed in url
    exit;
    } else {
    //create url to use in curl call
    $term = str_replace(" ", "_", $search);
    $url = "http://en.wikipedia.org/w/api.php?action=query&titles=".$term."&prop=images&format=json&imlimit=5";

    $json = curl($url);
    $results = getResults($json);

    //print the results using an unordered list
    echo "<ul>";
    foreach($results as $a){
    echo '<li><img src="'.$a.'"></li>';
    }
    echo "</ul>";
    }


    ?>