我正在使用此代码知道如何从我的网站获取谷歌图像..实际上问题是我没有从我的服务器获取谷歌图像阵列。我得到一个空数组。 我在我的localhost中运行此代码。它工作正常。但不是在我的服务器上。得到403forbidden错误。在这个页面中,我在goImageUrls()下载了来自google.inc.php的图片。下面是 DownloadImagesFromGoogle.inc.php
<?php
/**
* To download images of mentioned celebrity/person from google images
* Strictly for educational/learning purpose only.
* Please do not use it for commercial purpose as it is illigal !!
*
* @author Rochak Chauhan
* @version 2.0
* @see No need of cURL
*/
class DownloadImagesFromGoogle {
private $sizeLimit=200;
private $imagesOf="Lindsay";
private $numberOfImages;
private $googleUrlArray=array();
/**
* Function to download images from images.google
*
* @param string $imagesOf
* @param int $numberOfImages [OPTIONAL]
* @param int $sizeLimit [OPTIONAL]
*/
public function __construct($imagesOf, $numberOfImages=40, $sizeLimit=0) {
$this->imagesOf=$imagesOf;
$this->numberOfImages=$numberOfImages;
$this->sizeLimit=$sizeLimit;
$this->googleUrlArray=$this->createGoogleUrl();
}
/**
* Function the create a list of google image urls
*
* @access private
* @return array
*/
private function createGoogleUrl() {
$imagesOf = $this->imagesOf;
$numberOfImages = $this->numberOfImages+20;
$numberOfPages = ($numberOfImages/20)+1;
$j=0;
for($i=0; $i<$numberOfPages; $i++ ) {
$returnArray[] = "http://images.google.co.in/images?q=".rawurlencode($imagesOf)."&hl=en&lr=&start=".$j."&sa=N&gbv=1";
$j += 20;
}
return $returnArray;
}
/**
* Function to download images from google
*
* @access public
* @return void
*/
public function downloadImages() {
$imgUrlArray=$this->getImageUrls($urlArray);
$today = date("Ymd H:i:s");
$today= strtotime($today);
$dir=$today;
@mkdir($dir,0755);
@chmod($dir,0755);
$_SESSION[directory]=$dir;
//$insert=mysql_query("insert into googlefolders(foldername) values('$_SESSION[directory]')");
for($i=0; $i<count($imgUrlArray); $i++) {
$imageName = basename($imgUrlArray[$i]);
$info = @getimagesize($imgUrlArray[$i]);
if(trim($this->sizeLimit) != "" && $this->sizeLimit > 0) {
if (count($info) > 0 && $info[0] >= $this->sizeLimit) {
if(trim($imageName) != '' ) {
copy($imgUrlArray[$i], $dir."/".$imageName);
}
}
}
else {
if(trim($imageName) != '' ) {
copy($imgUrlArray[$i], $dir."/".$imageName);
}
}
}
}
/**
* Function to return paths of all images to be downloaded
*
* @access public
* @return array
*/
public function getImageUrls() {
$urlArray=$this->googleUrlArray;
$returnArray=array();
$returnArray1=array();
$return=array();
for ($j=0;$j<count($urlArray);$j++){
$url=trim($urlArray[$j]);
$str=$this->getHtmlCode($url);
echo $str;
$pattern='/<img src=(.*)\<\/a>/Us';
preg_match_all($pattern, $str, $returnArray, PREG_GREP_INVERT);
$returnArray1=$returnArray[1];
$count=count($returnArray1);
for ($i=1;$i<$count;$i++) {
$str1=trim(strip_tags($returnArray[1][$i]));
$pos1=strrpos($str1,"http://");
$pos2=strpos($str1," width");
$link=trim(substr($str1,$pos1,$pos2-$pos1));
$return[]=$link;
}
}
return $return;
}
/**
* Function to get source code of a url
*
* @param string $url
* @access private
* @return string
*/
private function getHtmlCode($url){
$returnStr="";
$fp=fopen($url, "r");
while (!feof($fp)) {
$returnStr.=fgetc($fp);
}
fclose($fp);
return $returnStr;
}
}
test.php的
<?php
set_time_limit(0);
ini_set("allow_url_fopen",1);
require_once('DownloadImagesFromGoogle.inc.php');
$downloadImagesFromGoogle = new DownloadImagesFromGoogle("rings", 40, 350);
$downloadImagesFromGoogle->downloadImages();
?>
答案 0 :(得分:0)
(403)Forbidden是与SSL相关的错误。您可以查看php身份验证和授权主题。确保图片网址不是https,否则需要ssl客户端接受。