0
我使用谷歌圖像api來檢索一些關於圖像的信息。唯一的問題是瞭解start
param。這是我的主循環:通過圖像循環
// $num - necessary number of images to find
function doGoogle($query, $num)
{
// Search images until we have need number
$i = $k = $n = 0;
while(count($resultStorage) < $num)
{
$base_url = 'http://ajax.googleapis.com/ajax/services/search/images?v=1.0';
$url = $base_url;
if ($i == 0)
$url .= '&imgsz=large&q='.urlencode($query).'&start=00';
else
$url .= '&imgsz=large&q='.urlencode($query).'&start='.$k.$n;
// Request the page from google's api
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, $url);
$json = curl_exec($curl);
curl_close($curl);
$data = json_decode($json);
echo '<div style="clear: both"></div>Page: <b>' . $i . '</b><br />Url: <b>' . $url . '</b><br />';
// Cycle through the list of result images
foreach($data->responseData->results as $v)
{
// If we already have neccessary number of images
if (count($resultStorage) == $num) break;
// Show it up
echo "<div style='float: left'><a href='{$v->unescapedUrl}'><img src='{$v->tbUrl}' /></a></div>";
}
$i++;
$k++;
if ($k == 9) { $k = 0; $n++; }
}
};
如何與n
和k
操縱?因爲有時它會返回相同的圖片(循環)。
您可以在頁面找到我的示例代碼:http://95.134.128.173/image.uploader.php(只需單擊「執行搜索」)。
Loook at code snippet。有'$ n; $ k'。 – Ockonal 2011-05-14 06:30:42
我在幫助你,請不要聽那種口氣。 – unknowndomain 2011-06-04 00:41:16