卷曲饼干存储
Curl cookies storage
我编写了一个 PHP
脚本来模拟 Web 浏览器。基本上,它的工作是 fetch
一个 Url
,存储关联的 cookies
并解析它以获取一些数据。
一直到周一下午还好好的,没想到在需要的目录下不再存储cookies
了。我假设我的代码没有更新或修改。
cookies
应该去的目录有 CHMOD 755
。
这是我目前使用的代码:
$options = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HEADER => 0,
CURLOPT_FAILONERROR => 1,
CURLOPT_USERAGENT => $this->user_agent,
CURLOPT_CONNECTTIMEOUT => 30,
CURLOPT_TIMEOUT => 30,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_MAXREDIRS => 10,
CURLOPT_AUTOREFERER => 1,
CURLOPT_COOKIESESSION => $reset_cookies ? 1 : 0,
CURLOPT_COOKIEJAR => $this->cookies_file,
CURLOPT_COOKIEFILE => $this->cookies_file,
CURLOPT_SSL_CIPHER_LIST => 'TLSv1',
);
// Add headers
if (isset($custom_headers))
{
$options[CURLOPT_HTTPHEADER] = $custom_headers;
}
// Add POST data
if (isset($post_data))
{
$options[CURLOPT_POST] = 1;
$options[CURLOPT_POSTFIELDS] = http_build_query($post_data);
}
// Attach options
curl_setopt_array($this->curl, $options);
// Execute the request and read the response
$content = curl_exec($this->curl);
// Handle any error
if (curl_errno($this->curl)) throw new Exception(curl_error($this->curl));
return $content;
要根据当前会话 ID
创建 cookie
文件,我使用此代码:
// Create a cookie file
$this->cookies_file = dirname(__FILE__) . '/../cookies/' . $this->session_id .'.cookie';
if (!file_exists($this->cookies_file)) file_put_contents($this->cookies_file, '');
你知道这是什么以及为什么会发生这种情况吗?此外,它在 WAMP
.
解决方案是将 cookies directory
权限设置为 644
。