Admin Admin
Mensajes : 36 Fecha de inscripción : 17/09/2011
| Tema: Buscar imágenes con google y php Miér Oct 12, 2011 11:01 am | |
| Esta original y práctico script nos busca imágenes relacionadas con la palabra de busqueda. Para ello utiliza el buscador de imágenes de google. Las utilidades que se le pueden dar son infinitas. Por ejemplo para postear noticias con su imagen relacionada, pra realizar búsquedas de modo remoto, etc. Muy bueno para trabajar con bases de datos. - Código:
-
<?php //get the word submitted from the form $word = "universo"; $img_pattern = "#<img src=http\S* width=[0-9]* height=[0-9]*>#"; // validate the word if ($word != '') { // initialise the session $ch = curl_init(); // Set the URL curl_setopt($ch, CURLOPT_URL, "http://images.google.com/images?gbv=1&hl=en&sa=1&q=".urlencode($word)."&btnG=Search+images"); // Return the output from the cURL session rather than displaying in the browser. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Execute the session, returning the results to $curlout, and close. $curlout = curl_exec($ch); curl_close($ch); preg_match_all($img_pattern, $curlout, $img_tags); //display the results - I'll leave the formatting to you print("Resultado de la busqueda $word: ".sizeof($img_tags[0])."<br/>\n"); foreach ($img_tags[0] as $val){ print(" ".$val."\n"); } } ?> | |
|