来自Google的随机图片网址

时间:2012-02-24 19:25:48

标签: php

我想知道是否有人知道如何在php中获取Google的随机图片网址? 感谢您的帮助和提前的时间。

5 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

  

首先下载并添加PHP Simple HTML DOM Parser,我希望它对某人有用。

<?php
require_once( "simplehtmldom.php" );

$image  = random_image( "life" );
if ( $image !== false ) {
  echo '<img src="'.$image.'" width="500" />';
}

function random_image( $key = "love" ) {
  $url  = sprintf( "https://www.google.com/search?q=%s&tbm=isch&sout=1", urlencode( trim( $key ) ) );
  $html = get_page( $url );
  if ( $html ) {
    $images = find_images( $html );
    if ( !empty( $images ) ) {
      $images = $images[array_rand( $images )];
      return $images;
    }
  }
  return false;
}

function get_page( $url = null ) {
  $url  = trim( $url );
  if ( !$url ) {
    return false;
  }
  return @file_get_contents( $url );
}

function find_images( $html = null ) {
  $html = trim( $html );
  if ( !$html ) {
    return false;
  }
  $html = str_get_html( $html );
  $imgs = array();
  if ( $links = $html->find( ".images_table td a" ) ) {
    foreach( $links as $link ) {
      $link = ( isset( $link->href ) ) ? $link->href : null;
      $link = htmlspecialchars_decode( parse_url( $link, PHP_URL_QUERY ) );
      parse_str( $link, $link );
      if ( isset( $link["imgurl"] ) ) {
        $imgs[] = urldecode( trim( $link["imgurl"] ) );
      }
    }
  }
  unset( $html );
  return $imgs;
}
?>

答案 2 :(得分:0)

到目前为止,我想到的最简单的方法是使用google image api(现在可免费使用):

function random_google_image($topic='') {
    $url = sprintf('http://ajax.googleapis.com/ajax/services/search/images?q=%s&v=1.0&rsz=large&start=1', urlencode($topic));
    $results = json_decode(file_get_contents($url))->responseData->results;
    $image = $results[array_rand($results)];
    return $image->url;
}

echo random_google_image('sexy girl');

Api请求获得前8个图像。注意api url中的start变量 - 你也可以随机化它以获得更多的可变性。还要检查$ image对象的所有属性,有一些可能有用的信息(取决于你的用例)。

要首先获得完全随机的图像,您应该定义您的主题,例如&#34;风景&#34;,&#34;图像&#34;,&#34;图片&#34;,&#34;地平线&#34 ;&#34; house&#34;,&#34; sky&#34;,&#34; sea&#34;然后将随机的一个传递给上面的函数。

答案 3 :(得分:0)

我刚刚写了这篇文章,当时我无法让上面的那些工作,我希望这有助于某人。这意味着您不需要包含任何其他项目(例如,简单的HTML DOM Parser),您可以按原样使用代码。

您需要在PHP中安装DOMDocument和XML功能。

https://github.com/damiankw/google.imagesearch

答案 4 :(得分:0)

<?php

function GetRandomImageURL($topic='', $min=0, $max=100)
{
  // get random image from Google
  if ($topic=='') $topic='image';
  $ofs=mt_rand($min, $max);
  $geturl='http://www.google.ca/images?q=' . $topic . '&start=' . $ofs . '&gbv=1';
  $data=file_get_contents($geturl);
 

 //partialString1 is bigger link.. in it will be a scr for the beginning of the url
  $f1='<div class="lIMUZd"><div><table class="TxbwNb"><tr><td><a href="/url?q=';
  $pos1=strpos($data, $f1)+strlen($f1);
  $partialString1 = substr($data, $pos1);
  
  //partialString 2 starts with the URL
  $f2='src="';
  $pos2=strpos($partialString1, $f2)+strlen($f2);
  $partialString2 = substr($partialString1, $pos2, 400);
  
  //PartialString3 ends the url when it sees the "&amp;"
  $f3='&amp;';
  $urlLength=strpos($partialString2, $f3);
  $partialString3 = substr($partialString2, 0,  $urlLength);
  
  return $partialString3;

}
 
 echo GetRandomImageURL();

?>