使用SimpleHTMLDom通过谷歌搜索获得大图像


Using SimpleHTMLDom to get big Image throu google search

我需要显示一张被谷歌搜索到的大图像simple_html_dom.php

 include_once( "simple_html_dom.php" );
       $key="alexis";
       $html = new simple_html_dom();
       $html=file_get_html('http://images.google.com/images?as_q='. $key .'&hl=en&imgtbs=z&btnG=Search+Images&as_epq=&as_oq=&as_eq=&imgtype=&imgsz=m&imgw=&imgh=&imgar=&as_filetype=&imgc=&as_sitesearch=&as_rights=&safe=images&as_st=y');
 foreach($html->find('a') as $element) {
 preg_match('#(?:http://)?(http(s?)://([^'s]*)'.(jpg|gif|png))#', $element->href,  $imagelink);
 echo $imagelink['1'];
 }

结果:

http://beautiful-pics.org/wp-content/uploads/2011/06/Alexis_Bledel_4.jpghttp://liberalvaluesblog.com/wp-content/uploads/2009/03/alexis-bledel-gg02.jpghttp://cdn0.sbnation.com/imported_assets/119825/alexis.jpghttp://www.hdwallpapersarena.com/wp-content/uploads/2012/04/alexis-dziena-3163.jpghttp://www.nba.com/media/celtics/alexis-diary300422.jpghttp://beautiful-pics.org/wp-content/uploads/2011/06/Alexis_Bledel_5.jpghttp://www.popcrunch.com/wp-content/uploads/2009/03/amanda-alexis-lee2.jpghttp://www.tvfunspot.com/forums/images/realityshows/survivor16/alexis.jpghttp://access.nscpcdn.com/gallery/i/b/bledel/AlexisBlei_4705661.jpghttp://2.bp.blogspot.com/_UaLWp72nij4/S_cccrvmFsI/AAAAAAAAMM8/yjVXCl3sHT4/s1600/alexis-bledel.jpghttp://3.bp.blogspot.com/-RFJoXjDSnmQ/T4cPwZwgrjI/AAAAAAAABlY/aNlgZ3ZFknU/s1600/Alexis%252BBledel-03.jpghttp://images2.fanpop.com/images/photos/4200000/Alexis-Bledel-alexis-bledel-4218179-590-768.jpghttp://www.hawktime.com/sitebuildercontent/sitebuilderpictures/AlexisArguello.jpghttp://4.bp.blogspot.com/--iAuASq2RFc/TveqvJ2j8vI/AAAAAAAABvg/RzTmjASqK_0/s400/alexis-bledel.jpghttp://www.mtv.com/news/photos/a/american_idol/09/top36/10_alexis_grace.jpghttp://images.buddytv.com/articles/the-bachelor/images/alexis.jpghttp://cdn01.cdnwp.thefrisky.com/wp-content/uploads/2010/02/03/vanity-fair-020310-main.jpghttp://www.chorva.net/wp-content/uploads/2009/04/alexis-bledel.jpghttp://images2.fanpop.com/images/photos/3500000/Alexis-Bledel-alexis-bledel-3523783-454-600.jpghttp://www.pocandpoch.com/wp-content/gallery/alexis-and-lauren/alexis-bledel.jpg

但是我需要一个链接,仅用于第一张图片,谢谢

include_once( "simple_html_dom.php" );
       $key="alexis";
       $html = new simple_html_dom();
       $html=file_get_html('http://images.google.com/images?as_q='. $key .'&hl=en&imgtbs=z&btnG=Search+Images&as_epq=&as_oq=&as_eq=&imgtype=&imgsz=m&imgw=&imgh=&imgar=&as_filetype=&imgc=&as_sitesearch=&as_rights=&safe=images&as_st=y');
 foreach($html->find('a') as $element) {
 if(preg_match('#(?:http://)?(http(s?)://([^'s]*)'.(jpg|gif|png))#', $element->href,  $imagelink)){
    echo $imagelink[1];
    break;
 }
 }

带有echo的行之后添加一个break ;

...
echo $imagelink['1'];
break;
}