我尝试制作一个来自并编写了一个从谷歌获取图片的脚本,但它没有显示任何图像,也没有给出任何错误我应该怎么做。 这是我的代码。
<html>
<head>
<title>Images</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="Post">
<table border="1">
<tr>
<td>Text:</td>
<td><input type="text" name="image" id="ImageType" value=""></td>
<td><input type="Submit" value="Click to Submit"></td>
</tr>
</table>
</form>
<br>
</body>
</html>
<?php
include "simple_html_dom.php";
if(isset($_POST['submit']))
{
$search_query = $_POST['image'];
// $search_query = "love";
$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 = $html->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;
}
}
?>
答案 0 :(得分:1)
$html = file_get_html( "https://www.google.com/search?q=$search_query& tbm=isch" );
应该是
$html = file_get_html( "https://www.google.com/search?q=$search_query&tbm=isch" );
需要删除search_query&
和tbm
之间的空格。
修改强>
<?php
if (isset($_POST['submit'])) {
include "simple_html_dom.php";
$search_query = $_POST['image'];
$html = file_get_html("https://www.google.com/search?q=$search_query&tbm=isch");
$images = $html->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;
}
}
?>
编辑 - 2 你应该给name属性提交按钮,如:
<input type="Submit" name="submit" value="Click to Submit">
或使用:
if (isset($_POST['image'])) {
代替if (isset($_POST['submit'])) {