Ver Mensaje Individual
  #1 (permalink)  
Antiguo 19/05/2014, 18:35
rodrigow
 
Fecha de Ingreso: septiembre-2008
Mensajes: 66
Antigüedad: 15 años, 8 meses
Puntos: 0
Buscar imagenes en google

Hola gente, cómo andan tanto tiempo? Vuelvo con un problemita con este código que saque de Google Code - Code Playground. Quiero poner en mi página una imagen dada una búsqueda en google imagenes que se realiza mediante este javascript:

/*
* How to search for images and restrict them by size.
* This demo will also show how to use Raw Searchers, aka a searcher that is
* not attached to a SearchControl. Thus, we will handle and draw the results
* manually.
*/

google.load('search', '1');

function searchComplete(searcher) {
alert(searcher);
// Check that we got results
if (searcher.results && searcher.results.length > 0) {
// Grab our content div, clear it.
var contentDiv = document.getElementById('content2');
contentDiv.innerHTML = '';

// Loop through our results, printing them to the page.
var results = searcher.results;
//for (var i = 0; i < results.length; i++) {
for (var i = 0; i < 1; i++) {
// For each result write it's title and image to the screen
var result = results[i];
var imgContainer = document.createElement('div');

var title = document.createElement('h2');
// We use titleNoFormatting so that no HTML tags are left in the title
title.innerHTML = result.titleNoFormatting;

var newImg = document.createElement('img');
// There is also a result.url property which has the escaped version
newImg.src = result.tbUrl;

//imgContainer.appendChild(title);
imgContainer.appendChild(newImg);

// Put our title + image in the content
contentDiv.appendChild(imgContainer);
}
}
}

function OnLoad() {
// Our ImageSearch instance.
var imageSearch = new google.search.ImageSearch();

// Restrict to extra large images only
imageSearch.setRestriction(google.search.ImageSear ch.RESTRICT_IMAGESIZE,
google.search.ImageSearch.IMAGESIZE_MEDIUM);

// Here we set a callback so that anytime a search is executed, it will call
// the searchComplete function and pass it our ImageSearch searcher.
// When a search completes, our ImageSearch object is automatically
// populated with the results.
imageSearch.setSearchCompleteCallback(this, searchComplete, [imageSearch]);


// Find me a beautiful car.
imageSearch.execute("metallica");
}
google.setOnLoadCallback(OnLoad);


-------------------------------------------------------------------------------------------------
Hasta funciona el código, me trae la primer imagen del resultado "metallica" y me lo muestra en el div con id "content2". Pero lo que quiero hacer es que metallica sea pasado mediante una variable en php. Cómo puedo hacer esto? Intenté poner imageSearch.execute(<?php echo $variable; ?>); pero no funciona. No entiendo bien cómo funciona todo esto. Ayuda please :)