0

我正在尝试使用 cURL 登录论坛并将 cookie 保存到文件中,然后解析 cookie 以获取phpbb2mysql_sidcookie。到目前为止,这是我的代码:

<?php

$curl = curl_init();

$cookieFile = 'C:\xampp\htdocs\cookies\\' . uniqid(true);

// Login
curl_setopt($curl, CURLOPT_URL, 'http://localhost/phpbb2/login.php');
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookieFile);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_POST, true);
$postVars = array('username' => 'username', 'password' => 'testing', 'autologin' => 'on', 'login' => 'Log in');
curl_setopt($curl, CURLOPT_POSTFIELDS, $postVars);
$resp = curl_exec($curl);

// Parse sid from cookie file
preg_match('/phpbb2mysql_sid\t(.*)/', file_get_contents($cookieFile), $match);
$sId = $match[1];

echo $sId;

但是,当我运行脚本时出现此错误:

Warning: file_get_contents(C:\xampp\htdocs\cookies\150367764c4ef3) [function.file-get-contents]: failed to open stream: No such file or directory in C:\xampp\htdocs\leech\post.php on line 18

Notice: Undefined offset: 1 in C:\xampp\htdocs\post.php on line 19

如果我检查我的文件系统,文件就在那里。获取文件内容的代码是cURL 请求之后,所以文件应该存在,对吧?

4

1 回答 1

1

发现了问题。cookie 文件只保存一次curl_close()被调用。只需在调用之前关闭 cURL 句柄file_get_contents(),它就会像一个魅力一样工作。

于 2012-08-23T18:38:20.883 回答