使用 php 获取 Google 搜索图片
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16457597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Google Search Images using php
提问by Tony Stark
Search on Google images with car keyword & get car images.
使用汽车关键字在 Google 图片上搜索并获取汽车图片。
I found two links to implement like this,
我找到了两个链接来实现这样的,
- PHP class to retrieve multiple images from Google using curl multi handler
- Google image API using cURL
implement also but it gave 4 random images not more than that.
也实现了,但它只给出了 4 个随机图像。
Question:How to get car images in PHP using keyword i want to implement like we search on Google?
问题:如何使用我想实现的关键字在 PHP 中获取汽车图像,就像我们在 Google 上搜索一样?
Any suggestion will be appreciated!!!
任何建议将不胜感激!!!
回答by Tony Stark
You could use the PHP Simple HTML DOM libraryfor this:
您可以为此使用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;
}
This will print a specific number of images (number is set in '$image_count').
这将打印特定数量的图像(数量在 '$image_count' 中设置)。
For more information on the PHP Simple HTML DOM library click here.
有关 PHP 简单 HTML DOM 库的更多信息,请单击此处。
回答by Tony Stark
i am not very much sure about this ,but still google gives a nice documentation about this.
我对此不太确定,但谷歌仍然提供了一个很好的文档。
$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...
this is from the official Google's developer guide regarding image searching. for more reference you can have a reference of the same here.
这是来自官方谷歌关于图像搜索的开发人员指南。如需更多参考,您可以在此处获得相同的参考。
https://developers.google.com/image-search/v1/jsondevguide#json_snippets_php
in $url you must set the search keywords.
在 $url 中,您必须设置搜索关键字。
回答by Guido Celada
I dont know if this will work for you, but you can try a technique called webpage scrapping. Find more about in this question:
我不知道这是否适合您,但您可以尝试一种称为网页抓取的技术。在这个问题中找到更多关于: