我正在尝试使用此代码抓取Google Images并将其返回到JSON数组,此代码工作正常,但如果我使用if $img > 0
,则第一个结果(0节点)始终返回Null,
有人可以告诉我如何解决此问题吗?
$search_query = "ginger pie cake site:pinterest.com";
$search_query = urlencode( $search_query );
$goourl = "https://www.google.com/search?q=".$search_query."&client=firefox-b-ab&tbs=isz:l,itp:photo&tbm=isch&sa=G&tbas=0&biw=1608&bih=817&dpr=0.9#imgrc=_";
$ch = curl_init($goourl);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Linux i686; rv:20.0) Gecko/20121230 Firefox/20.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$output = curl_exec($ch);
$array_getimg = explode("\"ou\":\"", $output);
$sresult = [];
foreach($array_getimg as $img => $imgurl){
if ($img > 0)
{
$array_getimg_2 = explode("\",\"", $imgurl);
$item['img'] = $array_imgurl[] = $array_getimg_2[0];
}
$sresult[] = $item;
}
var_dump($sresult);