2012-08-12 19 views
-1

下面的腳本從谷歌獲取的圖像,它只能得到20 $ page變量指定的頁面圖像。取出由谷歌圖片搜索結果時,如何獲得超過20的圖像?

我也沒弄明白爲什麼它恰好有個結果,我怎樣才能改變這個值要大,顯示例如100個第一圖像

<?php 


// Image sizes 
define ('GIS_LARGE', 'l'); 
define ('GIS_MEDIUM', 'm'); 
define ('GIS_ICON', 'i'); 
define ('GIS_ANY', ''); 

// Image types 
define ('GIS_FACE', 'face'); 
define ('GIS_PHOTO', 'photo'); 
define ('GIS_CLIPART', 'clipart'); 
define ('GIS_LINEART', 'lineart'); 

function get_data($url) 
{ 
$ch = curl_init(); 
$timeout = 5; 
curl_setopt($ch,CURLOPT_URL,$url); 
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout); 
$data = curl_exec($ch); 
curl_close($ch); 
return $data; 
} 


function googleImageSearch ($query, $page = 1, $size = GIS_ANY, $type = GIS_ANY) 
{ 

$retVal = array(); 

// Get the search results page 


$response = get_data("http://images.google.com/images?hl=en&q=" . urlencode ($query) . '&imgsz=' . $size . '&imgtype=' . $type . '&start=' . (($page - 1) * 21)); 

// Extract the image information. This is found inside of a javascript call to setResults 
preg_match('/\<table class=\"images_table\"(.*?)\>(.*?)\<\/table\>/is', $response, $match); 

if (isset($match[2])) { 

    // Grab all the arrays 
    preg_match_all('/\<td(.*?)\>(.*?)\<\/td\>/', $match[2], $m); 

    foreach ($m[2] as $item) { 

     // List of expressions used to grab all our info 
     $info = array(
      'resultLink' => '\<a href=\"(.*?)\"', 
      'source' => 'imgurl=(.*?)&amp;', 
      'title' => '\<br\/\>(.*?)\<br\/\>([\d]+)', 
      'width' => '([\d]+) &times;', 
      'height' => '&times; ([\d]+)', 
      'type' => '&nbsp;-([\w]+)', 
      'size' => ' - ([\d]+)', 
      'thumbsrc' => 'src="(.*?)"', 
      'thumbwidth' => 'width="([\d]+)"', 
      'thumbheight' => 'height="([\d]+)"', 
      'domain' => '\<cite title="(.*?)"\>' 
     ); 

     $t = new stdClass; 
     $t->thumb = new stdClass; 
     foreach ($info as $prop => $expr) { 
      if (preg_match('/' . $expr . '/is', $item, $m)) { 
       $value = 'title' == $prop ? str_replace(array('<b>', '</b>'), '', $m[1]) : $m[1]; 

       // Thumb properties go under the thumb object 
       if (0 === strpos($prop, 'thumb')) { 
        $prop = str_replace('thumb', '', $prop); 
        $t->thumb->$prop = $value; 
       } else { 
        $t->$prop = $value; 
       } 

       // Nicey up the google images result url 
       if ('resultLink' == 'resultLink') { 
        $t->resultLink = 'http://images.google.com' . $t->resultLink; 
       } 

      } 
     } 

     $retVal[] = $t; 

    } 

} 

return $retVal; 

} 

哪裏是代碼,告訴行腳本獲得20張圖片?

任何幫助將不勝感激。

回答

1

好了,你不能。該腳本正在從標準版本的Google圖像中獲取結果,並且無法更改每個頁面的結果。你唯一能做的就是要求五次共有100張圖像。

更新:爲了不斷更新附加圖像,只需使用'+'運算符。像,

$image = array(); 

for($i = 1; $i <= 5; $i++) 
    $image += googleImageSearch ($query, $page = 1, $size = GIS_ANY, $type = GIS_ANY); 

當心,如果你不聰明的掩飾了自己的要求,或谷歌是可疑的自動化要求你很可能會遇到這個頁面。


enter image description here

+0

我也是我思考再三調用該函數,可以請你告訴我我怎樣才能使這個功能做的工作2次,所以它返回之前到$ retVal的[]每次追加? 我吸上這三天沒辦法解決。您的幫助將不勝感激。 – 2012-08-12 09:36:55