Articles about programming
29-05-2013
https://www.google.com/cse/publicurl?cx=0123456789:xug12345sgzj9
Write down the "cx" parameter value for later. Now select "Modify your search engine - Control Panel." In the options page, go to "Image Search", select "Yes", so it will search in Google Images. In "Sites to search”, select "Search entire web but emphasize included sites." Once done, you must create an account in Google Developers Console, if we don't have one.
https://www.googleapis.com/customsearch/v1?googlehost=google.co.uk&lr=lang_en&imgType=photo&imgSize=medium&num=1&cr=countryUK&searchType=image&key=AIzaabcdefghijklmnopqrstuvwxyz1234&cx=123456789:w5tfght45rt6y7u&q=keyword+to+search
This give us a JSON with the result, that we can parse with PHP. Let's make a script to do a query on the database, get the name or the field that we will use to search for images from each row, and it will save the name of the image that gives us the Google search in the row in the field "image". Using this method we will have all the rows with the "image" field updated with the name of an actual image.
$link=mysql_connect("HOST", "USER", "PASSWORD");
$db_selected = mysql_select_db('DATABASE', $link);
$res = mysql_query("select id,name from table limit 100");
while ($row = mysql_fetch_array($res)) {
if ($row['name']) {
$term=urlencode(charnochars($row['name']).", London");
$ch = curl_init();
$timeout = 0;
curl_setopt ($ch, CURLOPT_URL, "https://www.googleapis.com/customsearch/v1?googlehost=google.co.uk&lr=lang_en&imgType=photo&imgSize=large&num=1&cr=countryUK&searchType=image&key=MY_API_KEY&cx=MY_ID&q=".$term);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$content = curl_exec($ch);
curl_close($ch);
$ch=null;
$json=json_decode($content);
if ($json->items[0]->link) {
$bits = explode('/', $json->items[0]->link);
$sql = "UPDATE table SET imagen ='".$bits[count($bits)-1]."' WHERE id =".$row['id'];
mysql_query($sql);
echo $json->items[0]->link."<br />";
}
}
sleep(1); // we pause the script 1 second, you can delete this
}
mysql_free_result($res);
function charnochars($search) {
$chars = array("À" => "A","Â" => "A","Ä" => "A","Æ" => "AE","È" => "E","Ê" => "E","Ì" => "I","Î" => "I","Ð" => "D","Ò" => "O","Ô" => "O","Ö" => "O","Ø" => "O",
"Ú" => "U","Ü" => "U","à" => "a","â" => "a","ä" => "a","æ" => "ae","è" => "e","ê" => "e","ì" => "i","î" => "i","ð" => "o","ò" => "o","ô" => "o","ö" => "o",
"ø" => "o","ú" => "u","ü" => "u","Á" => "A","Ã" => "A","Å" => "A","Ç" => "C","É" => "E","Ë" => "E","Í" => "I","Ï" => "I","Ñ" => "N","Ó" => "O","Õ" => "O",
"Ù" => "U","Û" => "U","Ý" => "Y","ß" => "B","á" => "a","ã" => "a","å" => "a","ç" => "c","é" => "e","ë" => "e","í" => "i","ï" => "i","ñ" => "n","ó" => "o",
"õ" => "o","ù" => "u","û" => "u","ý" => "y","ÿ" => "y");
return str_replace(array_keys($chars),$chars,$search);
}
Some words about this script:"For CSE users, the API provides 100 search queries per day for free. If you need more, you may sign up for billing in the Cloud Console. Additional requests cost $5 per 1000 queries, up to 10k queries per day."- I use the "charnochars" function to remove all special characters in keyword search. Although we use urlencode, Google does not like “ñ” and other special characters. No matter, for Google, it is the same “España" or "espana". Beware also of the script encoding (utf-8, iso latin, etc.), which can make this function to work incorrectly.
Leave a comment
We use cookies to ensure that we give the best user experience on our website. If you continue using this site we will assume that you agree with this. More info: Cookies Policy