PHP可以';在cURL请求后找不到cookie文件


PHP can't find cookie file after cURL request

我正试图使用cURL登录论坛,并将cookie保存到一个文件中,然后解析cookie以获得phpbb2mysql_sid cookie。这是我迄今为止的代码:

<?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请求之后的,所以文件应该存在,对吧?

发现问题。cookie文件仅在调用curl_close()后保存。只需在file_get_contents()调用之前关闭cURL句柄,它就可以像符咒一样工作。