使用php获取Google搜索图片

Ton*_*ark 4 php curl google-image-search

使用car关键字搜索Google图片并获取汽车图片.

在此输入图像描述

我找到了两个像这样实现的链接,

  1. PHP类使用curl多处理程序从Google检索多个图像
  2. 使用cURL的Google图片API

实施也,但它给了4个随机图像不超过这个. 在此输入图像描述在此输入图像描述在此输入图像描述在此输入图像描述

问题:如何使用关键字在PHP中获取汽车图像我想像我们在Google上搜索一样实现?

任何建议将不胜感激!

小智 5

您可以使用PHP Simple HTML DOM库:

<?php
    include "simple_html_dom.php";
    $search_query = "ENTER YOUR SEARCH QUERY HERE";
    $search_query = urlencode( $search_query );
    $html = file_get_html( "https://www.google.com/search?q=$search_query&tbm=isch" );
    $image_container = $html->find('div#rcnt', 0);
    $images = $image_container->find('img');
    $image_count = 10; //Enter the amount of images to be shown
    $i = 0;
    foreach($images as $image){
        if($i == $image_count) break;
        $i++;
        // DO with the image whatever you want here (the image element is '$image'):
        echo $image;
    }
Run Code Online (Sandbox Code Playgroud)

这将打印特定数量的图像(数字在'$ image_count'中设置).

有关PHP Simple HTML DOM库的更多信息,请单击此处.


小智 0

我对此不太确定,但谷歌仍然提供了关于此的很好的文档。

 $url = "https://ajax.googleapis.com/ajax/services/search/images?" .
   "v=1.0&q=barack%20obama&userip=INSERT-USER-IP";   
// sendRequest
// note how referer is set manually
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, /* Enter the URL of your site here */);
$body = curl_exec($ch);
curl_close($ch);

// now, process the JSON string
$json = json_decode($body);
// now have some fun with the results...
Run Code Online (Sandbox Code Playgroud)

这是来自谷歌官方关于图像搜索的开发者指南。如需更多参考,您可以在此处获得相同的参考。

 https://developers.google.com/image-search/v1/jsondevguide#json_snippets_php
Run Code Online (Sandbox Code Playgroud)

在 $url 中您必须设置搜索关键字。

  • Google 图片搜索 API 已于 2011 年 5 月 26 日正式弃用。 (4认同)