0

さて、これはちょっと気が狂います...Curlで外部FTP接続に接続しようとすると「内部サーバーエラー」が表示され続けます。FTPには正常にアクセスできますが、Curl経由ではアクセスできません。

私はCodeIgniterをラッパーとして使用していますが、それがそのような問題を引き起こすとは本当に思いません。メモリ/タイムアウトを増やしてみましたが、それでもアクセスできません。500内部サーバーエラーが実際に私のページにあります。Curlが何かを返しているかどうかはわかりませんが、「ユーザー名」を無効にするか、「パスワード」を追加しようとすると、Curlを介して通常のエラー(内部サーバーエラーではない)が発生することはわかっています(このFTPログインのパスワードはありません)。

これが私の主なスクリプトです:

function FTPScrape() {
    parent::Controller();
    $this->load->model("Curl_m");
}

function index() {
    ini_set('memory_limit', '70000000M');
    set_time_limit(0);
    define('COOKIES_DIR', 'cookies');
    define('RANDOM', COOKIES_DIR . '/' . md5(uniqid(mt_rand(), true)));
    $this->Curl_m->init(TRUE, RANDOM . '.txt');
    $this->Curl_m->start();

    $referer = '';
    $url = 'ftp://ftp.server.com/';
    $str = $this->Curl_m->ftp($url, 'user', '', __line__, $referer);
    print "<br><br><textarea cols=\"80\" rows=\"20\">{$str}</textarea><br><br>";
    $this->Curl_m->close();
}

私が使用する「Curl_m」モデル関数は次のとおりです。

function init($cookies = TRUE, $cookie = 'cookies.txt', $compression = '', $proxy = '') {
if(!is_dir('files')) {
    mkdir('files', 0777);
}
if(!is_dir('cookies')) {
    mkdir('cookies', 0777);
}
else if($dh = opendir('files/')) {
    while(($file = readdir($dh)) !== false) {
        if(is_file('files/'.$file)) {
            unlink('files/'.$file);
        }
    }
}
$this->user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 (.NET CLR 3.5.30729)';
$this->compression = $compression;
$this->proxy = $proxy;
$this->cookies = $cookies;
$this->filenumber = 0;
$this->follow_location = 1;
$this->headers_html = 0;
$this->binary = 0;
if($this->cookies == TRUE) {
    $this->cookie($cookie);
}
}

function start() {
    $this->process = curl_init();
}

function close() {
    curl_close($this->process);
}

function ftp($url, $user = '', $pass = '', $line_no = '', $referer = '') {
    return $this->execute($url, '', $line_no, $referer, $user, $pass);
}

function execute($url, $data = '', $line_no = '', $referer = '', $user = '', $pass = '') {
    if(isset($this->headers)) {
        unset($this->headers);
    }
    if(preg_match('/\w/xsi', $data)) {
        $type = 'POST';
    }
    else {
        $type = 'GET';
    }

    $host = parse_url($url);
    $this->headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
    $this->headers[] = 'Accept-Language: en-us,en;q=0.5';
    $this->headers[] = 'Accept-Encoding: gzip,deflate';
    $this->headers[] = 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7';
    $this->headers[] = 'Keep-Alive: 300';
    $this->headers[] = 'Connection: keep-alive';
    $this->headers[] = 'Expect:';
    if(preg_match('/\w/xsi',  $referer)) {
        $this->headers[] = 'Referer: ' . $referer . '';
    }

    if($type == 'POST') {
        if(isset($this->Content_type_change)) {
            $this->headers[] = 'Content-Type: ' . $this->Content_type_change . '';
            unset($this->Content_type_change);
        }
        else {
            $this->headers[] = 'Content-Type: application/x-www-form-urlencoded';
        }
        if(isset($this->extra_headings)) {
            $this->headers[] = $this->extra_headings;
        }
        if(!preg_match('/https:/xsi', $url)) {
            $this->headers[] = 'Content-Length: ' . strlen($data) . '';
        }
    }

    curl_setopt($this->process, CURLOPT_URL, $url);
    curl_setopt($this->process, CURLOPT_HTTPHEADER, $this->headers);
    curl_setopt($this->process, CURLOPT_HEADER, $this->headers_html);
    curl_setopt($this->process, CURLOPT_USERAGENT, $this->user_agent);
    if($this->cookies == TRUE) {
        curl_setopt($this->process, CURLOPT_COOKIEFILE, $this->cookie_file);
    }
    if($this->cookies == TRUE) {
        curl_setopt($this->process, CURLOPT_COOKIEJAR, $this->cookie_file);
    }
    curl_setopt($this->process, CURLOPT_ENCODING, $this->compression);
    curl_setopt($this->process, CURLOPT_CONNECTTIMEOUT, 0);
    curl_setopt($this->process, CURLOPT_TIMEOUT, 1200);
    curl_setopt($this->process, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($this->process, CURLOPT_RETURNTRANSFER, 1);

    if(strpos("ftp", $url) !== false) {
        curl_setopt($this->process, CURLOPT_FTPLISTONLY, 1);
    }
    if(!empty($user)) {
        $usrpwd = $user . ':' . $pass;
        curl_setopt($this->process, CURLOPT_USERPWD, $usrpwd);
    }

    if($this->binary == 1) {
        curl_setopt($this->process, CURLOPT_BINARYTRANSFER, 1);
    }

    if($this->follow_location == 1) {
        curl_setopt($this->process, CURLOPT_FOLLOWLOCATION, 1);
    }
    else {
        curl_setopt($this->process, CURLOPT_FOLLOWLOCATION, 0);
    }

    if($type == 'POST') {
        curl_setopt($this->process, CURLOPT_POSTFIELDS, $data);
        curl_setopt($this->process, CURLOPT_POST, 1);
    }

    if(preg_match("/\w/", $this->proxy)) {
        curl_setopt($this->process, CURLOPT_PROXY, $this->proxy);
    }

    $return = curl_exec($this->process);
    if($this->file_extension($url,$return) == 'jpg') {
        $fh = fopen('captcha.jpg', "w");
        fwrite($fh, $return);
        fclose($fh);
    }
    unset($this->headers);
    return $return;
}

なぜ私がこの問題を抱えているのか誰かが知っていますか?

ほとんどのスクリプトは、このプロジェクトを開始する前に作成されました(つまり、Curl_mモデルの関数)。クラスを実際のcodeigniterモデルに変換しただけです。

これが内部サーバーエラーを引き起こすのを防ぐ方法を理解できれば、残りの部分は簡単に修正できるはずです。

4

1 に答える 1

0

さて、なぜそれが正しく機能しなかったのか理解しました。許可されるメモリを増やして無制限のタイムアウトを許可しようとしましたが、サーバーがそれを許可したくなかったと思います。

昨日と今日、クライアントが設定したAmazon EC2アカウントを構成して、このすべての情報をそこに配置できるようにした後、FTP Scraperを再度テストすると、接続に成功し、アクセスしようとしたデータを取得しました。とにかく、私のサーバーは実際にはテスト専用でした。最終的なスクリプトはAmazon EC2に配置される予定でしたが、セットアップが複雑なため、延期していました(これまで、Amazon EC2を使用したことはありませんでした)。

いずれにせよ、問題は、スクリプトがタイムアウトするか、割り当てられたメモリを超えていることでした。ハイエンドサーバーにセットアップすることで、接続が正常に機能するようになりました。どうやら、Curlを使用してFTPにログインすると、思ったよりも多くのリソース/時間がかかります。

于 2011-07-06T21:29:02.363 に答える