The following is some very rough code in the attempt to grab images off google search and shove them in a mysql database.
State of code:
So far code works to the point that it will grab images and put the URL in the database then display the images(not extracted from database).
Need:
I would like to put the images themselves in the database but cannot find a successful way to do so. Any help is greatly appreciated.
Code: Select all
<?php
// get list of images from google images
// Create MySQL login values and
// set them to your login information.
$username = "*******";
$password = "**********";
$host = "*******";
$database = "binary";
// Make the connect to MySQL or die
// and display an error.
$link = mysql_connect($host, $username, $password);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
// Select your database
mysql_select_db ($database);
$k='*****some query****';
$url = "http://www.google.com/images?hl=en&source=imghp&biw=1920&bih=859&q=##query##&gbv=2&aq=f&aqi=g10&aql=&oq=&gs_rfai=";
$web_page = file_get_contents( str_replace("##query##",urlencode($k), $url ));
$tieni = stristr($web_page,"dyn.setResults(");
$tieni = str_replace( "dyn.setResults(","", str_replace(stristr($tieni,");"),"",$tieni) );
$tieni = str_replace("[]","",$tieni);
$m = preg_split("/[\[\]]/",$tieni);
$x = array();
for($i=0;$i<count($m);$i++) {
$m[$i] = str_replace("/imgres?imgurl\\x3d","",$m[$i]);
$m[$i] = str_replace(stristr($m[$i],"\\x26imgrefurl"),"",$m[$i]);
$m[$i] = preg_replace("/^\"/i","",$m[$i]);
$m[$i] = preg_replace("/^,/i","",$m[$i]);
if ($m[$i]!="")
{
$tmpName = $m[$i];
// Create the query and insert
// into our database.
$query = "INSERT INTO tbl_images ";
$query .= "(image) VALUES ('$tmpName')";
$results = mysql_query($query, $link);
//
echo "<img src='".$m[$i]."' />";
array_push($x,$m[$i]);
}
}
$ar = $x;
?>