来自谷歌的随机图像URL


Random Image URL From Google

我想知道是否有人知道如何在php中从谷歌获得随机图像URL?感谢您的帮助和提前安排的时间。

这将帮助您

  • http://www.php.net/manual/en/function.file-get-contents.php
  • http://php.net/manual/en/book.curl.php

首先下载并包含PHPSimpleHTMLDOMParser,我希望它对某些人有用。

<?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;
}
?>

到目前为止,我发现的最简单的方法是使用谷歌图像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中的起始变量-您也可以将其随机化以获得更多的可变性。还要检查$image对象的所有属性,其中有一些信息可能有用(取决于您的用例)。

要获得完全随机的图像,首先你应该定义你的主题,如"风景"、"图像"、"图片"、"地平线"、"房子"、"天空"、"大海"等。然后将随机一个传递到上面的函数。

我不久前写了这篇文章,当时我无法让上面的内容发挥作用,我希望这能帮助到别人。这意味着您不需要包含任何其他项目(例如SimpleHTMLDOMParser),只需按原样使用代码即可。

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

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

<?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();
?>