卷曲饼干存储


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