2010-11-25 19 views
0

私はこのコードを使用して、自分のサイトからGoogleイメージを取得する方法を知っています。実際には、問題は私のサーバーから配列にGoogleイメージを取得していません。私は空の配列を取得しています。 私はこのコードを自分のlocalhostで実行しました。それは正常に動作しています。私のサーバーではありません。 403forbiddenエラーが発生しました。このページでは、getImageUrls()のdownloadimagesfromgoogle.inc.phpでこのようにチェックしました。以下禁止 DownloadImagesFromGoogle.inc.phpGoogleエラー403 forbiddenエラー

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

誰かが返信してください。私は非常に多くをしようとしていますが、私は解決策が見つかりませんでした –

答えて

0

(403)は、SSL関連のエラーです。あなたはPHP認証と認可のトピックを見ているかもしれません。画像URLがhttpsでない場合は、SSLクライアントの受け入れが必要です。