我编写了一个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;
要根据当前会话cookie
创建ID
文件,我使用以下代码:
// 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
下完全适用于本地。
答案 0 :(得分:0)
解决方案是将cookies directory
权限设置为644
。