Last active
October 16, 2015 20:10
-
-
Save MuriloEduardo/5828b20d20c11ba69c49 to your computer and use it in GitHub Desktop.
Código para busca online de imagens, precisando somente informar uma string(à busca), como parâmetro.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
set_time_limit(0); | |
/* Sua busca */ | |
/* -------------------------------------------------- */ | |
/* - */ exibeBusca("produtos de supermercado"); /* - */ | |
/* -------------------------------------------------- */ | |
/* Use uma string normal, com acentos e espaços */ | |
/* | |
* Verifica se o arquivo no servidor existe, | |
* neste caso, verifica se a imagem existe | |
* retorna resposta xhr | |
*/ | |
function url_exists($url) { | |
$ch = curl_init($url); | |
curl_setopt($ch, CURLOPT_NOBODY, true); | |
curl_exec($ch); | |
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
curl_close($ch); | |
return ($code == 200); // verifica se recebe "status OK" | |
} | |
/* | |
* Obtem as imagens | |
*/ | |
function get_url_contents($url) { | |
$crl = curl_init(); | |
curl_setopt($crl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)'); | |
curl_setopt($crl, CURLOPT_URL, $url); | |
curl_setopt($crl, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($crl, CURLOPT_CONNECTTIMEOUT, 5); | |
$ret = curl_exec($crl); | |
curl_close($crl); | |
return $ret; | |
} | |
/* | |
* Recebe a string, e executa a função get_url_contents() | |
* retorna um Json com no máximo (int)4 imagens, caso a imagen esteja corrompida | |
* não será exibida | |
*/ | |
function exibeBusca($string_produto){ | |
$string_pronta_produto = str_replace(' ', '%20', $string_produto); | |
$json = get_url_contents('http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q='.$string_pronta_produto); | |
$data = json_decode($json); | |
foreach ($data->responseData->results as $result) { | |
$results[] = array('url' => $result->url, 'alt' => $result->title); | |
} | |
if (!empty($results)) { | |
for ($i=0; $i < count($results); $i++) { | |
if (url_exists($results[$i]["url"])){ | |
/* Retorna a url da imagen sem '.jpg(extenção)'*/ | |
$string = substr($results[$i]["url"], -strlen($string_produto), -4); | |
/* Retira caracteries especias*/ | |
$string = iconv( "UTF-8" , "ASCII//TRANSLIT//IGNORE" , $string ); | |
$string = preg_replace( array( '/[ ]/' , '/[^A-Za-z0-9\-]/' ) , array( '' , '' ) , $string ); | |
/* Exibe uma url por vez*/ | |
$string_for_class = $string.$i; | |
$url = $results[$i]["url"]; | |
echo "<div id='caixaFotoPrdt' class='" . $string_for_class . "'> | |
<img src='" . $url . "' width='155px' height='155px'> | |
</div>"; | |
} | |
} | |
} | |
} | |
/* | |
* Usa-se tratamentos de strings, para uma possivel ação de javascript | |
* na sua pagina que pegará essas imagens | |
*/ | |
?> |
Author
MuriloEduardo
commented
Oct 16, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment