<?php
function get1stGoogleImage($title)
{
	$rows = query("select * from cover where title='$title'");
	if (count($rows) == 1)
		return $rows[0][url];
	include_once('simple_html_dom.php');
	//$search_keyword= $a[q];//$_GET['name'];
	$search_keyword=str_replace(' ','+',$title);
	//$newhtml = file_get_html("https://www.google.com/search?q=".$search_keyword."&tbm=isch&gws_rd=cr&ei=16E0WMGSKYmisAHmp6b4Ag");
	//$newhtml = file_get_html("https://www.google.com/search?q=".$search_keyword."&tbm=isch");
	//file_put_contents("search.txt", $newhtml);
	$options = array(
	  'http'=>array(
		'method'=>"GET",
		'header'=>"Accept-language: en\r\n" .
				  //"User-Agent: Mozilla/1.22 (compatible; MSIE 10.0; Windows 3.1)Mozilla/1.22 (compatible; MSIE 10.0; Windows 3.1)\r\n" // IE10
				  "User-Agent: Mozilla/1.22 (compatible; MSIE 10.0; Windows 3.1)Mozilla/1.22 (compatible; MSIE 10.0; Windows 3.1)\r\n" // IE10
	  )
	);
	$context = stream_context_create($options);
	$url = "http://www.google.co.kr/search?hl=en&tbm=isch&source=hp&q=".$search_keyword;
	$html = file_get_contents($url, false, $context);
	$newhtml = str_get_html($html);
	//echo $newhtml;
	$result_image_source = $newhtml->find('.images_table', 0)->find('img', 0)->src;
	
	query("insert into cover (title, url) values (:title, :url)", array(
		title=>$title,
		url=>$result_image_source,
	));	
	return $result_image_source;
}


function setcookie8($key, $val)//8 means infinite
{
	setcookie($key, $val, 2147483647, '/');
}


?>