Tutorial: Retrieve Google image(s) randomly with PHP

There are two files with *.php extension that you need to retrieve a random image from Google Images and visually display it. However, you may also need to randomly show an image every few seconds on your main page, and an iframe will do the trick.

You will need a Google Image search file to include in your file. Download the gisearch.zip that contains the php file.

Once you downloaded that file, create a new file PHP file called images.php and add the standard HTML tags to it.

Show Google Images

PHP

1

2

3

4

5

6

7

8

9

10

11

12

13

14

<html>

<head>

<title>Image</title>

<meta http-equiv="refresh"content="10">

</head>

<body>

<?php

include("gisearch.php");

$i=newgisearch("small airplanes");//the search keyword

echo'<center><img src="'.$i->get_link().'"

style="height: 180px; max-width: 250px;"/></center><br>';

?>

</body>

</html>

*Notice at line 4, the page refreshes every 10 seconds with a refresh meta tag.

What if you don’t want the entire page by itself and want to include it on your main page. It’s best to use an iframe to load your images.php page that auto refreshes every 10 seconds.

10 Comments

Good, I improve to repeat it recursively, sometimes the string retrieves and try to show an image that doesn’t exist or is forbidden hotlinking, so I improve retrieving image size and repeating the function if is empty or zero. Cheers

I love answering comments! I try to as soon as I get them. Forget the rand 20 in gisearch.php. Put it back to 1.
I am sorry I should have double checked first and tested it before I advise you. When you display the image, let the new image be in a loop. I am not sure how great the browser can handle this, but try it and will solve the issue:

< ?php
include("gisearch.php");
for($h=0; $h<20; $h++){
$i = new gisearch("flowers"); //must be in the loop because you want a new image every time to display
echo ' < img src="'. $i->get_link() .'" >';
} ?>