0

curl を使用して Web サイトにログインする PHP スクリプトがあります。ブラウザでスクリプトを実行すると、スクリプトは正常にログインします。cronjob を使用して実行すると、Cookie が予期した場所に保存されないため、ログインしません。

Cookie の保存方法を教えてください。


これは、スクリプトの関連部分です。その前に、URL ($url) とログイン データ ($post_string) が定義されます。

class curl {
    function __construct($use = 1) {
        $this->ch = curl_init();
        if($use = 1) {
            curl_setopt ($this->ch, CURLOPT_POST, 1);
            curl_setopt ($this->ch, CURLOPT_COOKIEJAR, $_SERVER['DOCUMENT_ROOT'].'/verwaltung/cookie.txt');
            curl_setopt ($this->ch, CURLOPT_COOKIEFILE, $_SERVER['DOCUMENT_ROOT'].'/verwaltung/cookie.txt');
            curl_setopt ($this->ch, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt ($this->ch, CURLOPT_RETURNTRANSFER, 1);
        } else {
            return 'There is the possibility, that this script wont work';
        }
    }
    function first_connect($loginform,$logindata) {
        curl_setopt($this->ch, CURLOPT_URL, $loginform);
        curl_setopt ($this->ch, CURLOPT_POSTFIELDS, $logindata);
    }
    function store() {
        $store = curl_exec ($this->ch);
    }
    function execute($page) {
        curl_setopt($this->ch, CURLOPT_URL, $page);
        $this->content = curl_exec ($this->ch);
    }
    function close() {
        curl_close ($this->ch);
    }
    function __toString() {
        return $this->content;
    }
}

$getit = new curl();
$getit->first_connect($url, $post_string);
$getit->store();
$getit->execute($url);
$getit->close();

以下のWrikkenとColin Morelliのコメントを反映するように編集された質問。あなたがた両方に感謝します!

4

1 に答える 1

0

コマンドラインからスクリプトを実行すると、$_SERVER['DOCUMENT_ROOT']変数は空になります(結局のところ、サーバーもドキュメントルートもありません)。

これは、$_SERVER['DOCUMENT_ROOT'].'/verwaltung/cookie.txt'最終的には解決されることを意味します'/verwaltung/cookie.txt'(そしてPHPの通知)。このディレクトリはおそらく存在せず(ルートファイルシステムに直接存在します。デフォルトのUnixシステムディレクトリ以外には存在しないはずです)、スクリプトはCookieファイルを作成できません(その後、cURLによって設定されたCookieを保存できません)。 )。

の代わりに$_SERVER['DOCUMENT_ROOT']、たとえば.(現在のディレクトリ)、/tmp(すべてのユーザーが書き込み可能である必要があります。また、読み取り可能であることに注意してください)、__DIR__(PHPスクリプトが存在するディレクトリ)、またはその他の任意のディレクトリを使用できます。サーバー変数に依存しません。

于 2013-03-06T20:46:57.017 に答える