【发布时间】:2015-02-04 08:36:38
【问题描述】:
我编写了一个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 下的本地运行良好。
【问题讨论】: