我可以从 Google Search API 读取图像搜索结果,这些结果存储在 JSON 结果中,下面是其中的摘录,显示了搜索"elephant"时的第一个结果:
将 Json 存储在 $js 中:$js = json_decode($data,true);
var_dump json:var_dump($js);
["items"]=> array(10)
{ [0]=> array(9)
{
["kind"]=> string(19) "customsearch#result"
["title"]=> string(66) "African Elephants, African Elephant Pictures, African Elephant ..."
["htmlTitle"]=> string(94) "African Elephants, African Elephant Pictures, African Elephant ..."
["link"]=> string(105) "http://images.nationalgeographic.com/wpf/media-live/photos/000/004/cache/african-elephant_435_600x450.jpg"
["displayLink"]=> string(30) "animals.nationalgeographic.com"
["snippet"]=> string(62) "African Elephants, African Elephant Pictures, African Elephant"
["htmlSnippet"]=> string(83) "African Elephants, African Elephant Pictures, African Elephant"
["mime"]=> string(10) "image/jpeg"
["image"]=> array(7)
{
["contextLink"]=> string(71) "http://animals.nationalgeographic.com/animals/mammals/african-elephant/"
["height"]=> int(450) ["width"]=> int(600)
["byteSize"]=> int(55348)
["thumbnailLink"]=> string(111) "https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcT8395mqQZUPd2pxSPO7OEPwmjdp5KqtHCRaKzJgcMW_g5mwcEetosfmKU" ["thumbnailHeight"]=> int(101)
["thumbnailWidth"]=> int(135)
}
}
我将“项目”存储在一个数组中,并且可以使用以下代码输出标题、链接和描述(片段):
$googlearray= $js['items'];
$z=0;
foreach ($googlearray as $finallist)
{
$z++;
echo $z.": <a href=\"{$finallist['link']}\"><font color ='blue'>{$finallist['title']}</font></a>".": "."$newline"."$newline".$finallist['snippet'].$newline.$newline;
}
是否可以在网页上显示实际图像,而不仅仅是指向图像的链接?
多谢你们。