Google's AJAX Search API allows you to search for images: Video and Image Search Examples, though whether you could use PHP to parse the results I'm not sure. You could certainly parse the returned data to extract image urls to apply as backgrounds though. There is also a Yahoo Image Search API and flickr Search API to try.
The Google AJAX Documentation has a PHP code snippet that shows how to call searches using PHP.
Here's code that finds image search results for "batman":
$word = "batman";
$manual_referer = 'http://example.com/';
// See reference for how to modify search
// http://code.google.com/apis/ajaxsearch/documentation/reference.html
$args = array(
'v' => '1.0',
'q' => $word,
'as_filetype' => 'jpg',
'imgsz' => 'medium', // image size
'safe' => 'active', // image "safeness"
'as_filetype' => 'jpg',
);
$url = "http://ajax.googleapis.com/ajax/services/search/images?";
foreach ($args as $key => $val) {
$url .= $key . '=' . rawurlencode($val) . '&';
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $manual_referer);
$body = curl_exec($ch);
curl_close($ch);
$json = json_decode($body, true);
$results = $json['responseData']['results'];
foreach ($results as $result) {
print "<img src=";
print $result['url']; // here's your url
print ">";
}
Here's what the raw returned json looks like when decoded:
Array
(
[responseData] => Array
(
[results] => Array
(
[0] => Array
(
[GsearchResultClass] => GimageSearch
[width] => 240
[height] => 338
[imageId] => k8FYqFKsdhvu3M:
[tbWidth] => 84
[tbHeight] => 119
[unescapedUrl] => http://lizilla.files.wordpress.com/2009/08/batman.jpg
[url] => http://lizilla.files.wordpress.com/2009/08/batman.jpg
[visibleUrl] => lizilla.wordpress.com
[title] => Superhero's, Animation, Comics, And Interesting Movies. « Lizilla
[titleNoFormatting] => Superhero's, Animation, Comics, And Interesting Movies. « Lizilla
[originalContextUrl] => http://lizilla.wordpress.com/2009/08/26/superheros-animation-comics-and-interesting-movies/
[content] => Dunanununanuna <b>BATMAN</b>!
[contentNoFormatting] => Dunanununanuna BATMAN!
[tbUrl] => http://images.google.com/images?q=tbn:k8FYqFKsdhvu3M::lizilla.files.wordpress.com/2009/08/batman.jpg
)
[1] => Array
(
[GsearchResultClass] => GimageSearch
[width] => 307
[height] => 290
[imageId] => faxJ90Dbo1TW1M:
[tbWidth] => 117
[tbHeight] => 111
[unescapedUrl] => http://www.solarnavigator.net/films_movies_actors/film_images/batman_michael_keaton_jack_nocholson_joker_marvel_comics.jpg
[url] => http://www.solarnavigator.net/films_movies_actors/film_images/batman_michael_keaton_jack_nocholson_joker_marvel_comics.jpg
[visibleUrl] => www.solarnavigator.net
[title] => <b>BATMAN</b> THE MOVIE
[titleNoFormatting] => BATMAN THE MOVIE
[originalContextUrl] => http://www.solarnavigator.net/films_movies_actors/batman.htm
[content] => <b>Batman</b> and the Joker in the
[contentNoFormatting] => Batman and the Joker in the
[tbUrl] => http://images.google.com/images?q=tbn:faxJ90Dbo1TW1M::www.solarnavigator.net/films_movies_actors/film_images/batman_michael_keaton_jack_nocholson_joker_marvel_comics.jpg
)
[2] => Array
(
[GsearchResultClass] => GimageSearch
[width] => 300
[height] => 300
[imageId] => nDWzhPnraNi_gM:
[tbWidth] => 116
[tbHeight] => 116
[unescapedUrl] => http://i192.photobucket.com/albums/z167/Great_WhiteSnark/batman_bale-1.jpg
[url] => http://i192.photobucket.com/albums/z167/Great_WhiteSnark/batman_bale-1.jpg
[visibleUrl] => www.coolchaser.com
[title] => <b>batman</b> MySpace graphics and comments
[titleNoFormatting] => batman MySpace graphics and comments
[originalContextUrl] => http://www.coolchaser.com/graphics/tag/batman
[content] => All Graphics » <b>batman</b>
[contentNoFormatting] => All Graphics » batman
[tbUrl] => http://images.google.com/images?q=tbn:nDWzhPnraNi_gM::i192.photobucket.com/albums/z167/Great_WhiteSnark/batman_bale-1.jpg
)
[3] => Array
(
[GsearchResultClass] => GimageSearch
[width] => 250
[height] => 302
[imageId] => W9EAV1DUDesHuM:
[tbWidth] => 96
[tbHeight] => 116
[unescapedUrl] => http://upload.wikimedia.org/wikipedia/en/thumb/f/f6/New_Batman_Adventures_cast.jpg/250px-New_Batman_Adventures_cast.jpg
[url] => http://upload.wikimedia.org/wikipedia/en/thumb/f/f6/New_Batman_Adventures_cast.jpg/250px-New_Batman_Adventures_cast.jpg
[visibleUrl] => en.wikipedia.org
[title] => The New <b>Batman</b> Adventures - Wikipedia, the free encyclopedia
[titleNoFormatting] => The New Batman Adventures - Wikipedia, the free encyclopedia
[originalContextUrl] => http://en.wikipedia.org/wiki/The_New_Batman_Adventures
[content] => The New <b>Batman</b> Adventures
[contentNoFormatting] => The New Batman Adventures
[tbUrl] => http://images.google.com/images?q=tbn:W9EAV1DUDesHuM::upload.wikimedia.org/wikipedia/en/thumb/f/f6/New_Batman_Adventures_cast.jpg/250px-New_Batman_Adventures_cast.jpg
)
)
[cursor] => Array
(
[pages] => Array
(
[0] => Array
(
[start] => 0
[label] => 1
)
[1] => Array
(
[start] => 4
[label] => 2
)
[2] => Array
(
[start] => 8
[label] => 3
)
[3] => Array
(
[start] => 12
[label] => 4
)
[4] => Array
(
[start] => 16
[label] => 5
)
[5] => Array
(
[start] => 20
[label] => 6
)
[6] => Array
(
[start] => 24
[label] => 7
)
[7] => Array
(
[start] => 28
[label] => 8
)
)
[estimatedResultCount] => 3050000
[currentPageIndex] => 0
[moreResultsUrl] => http://www.google.com/images?oe=utf8&ie=utf8&source=uds&start=0&safe=active&imgsz=medium&as_filetype=jpg&hl=en&q=batman
)
)
[responseDetails] =>
[responseStatus] => 200
)
A tricky aspect of this is that you may need permission to use these images. In some testing it looks like when you restrict by public domain you get far fewer images in result. Also, there's no mechanism to restrict to servers that are actually available at that time, so sometimes you may get a valid url but no image will be available. It seems like a simple request, but it introduces other issues in url handling.