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のコメントを反映するように編集された質問。あなたがた両方に感謝します!