我需要使用cURL从网站获取数据,但我需要在我的请求标头中包含会话令牌。问题是,会话令牌存储在浏览器的本地存储中。由于cURL不是Web浏览器,我如何获取会话令牌?
编辑: 这是我的代码:
define('COOKIE_FILE','cookie.txt');
$url = 'https://website.com';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIE_FILE);
curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIE_FILE);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curl_error = curl_error($ch);
curl_close($ch);
if ($curl_error || $http_code !== 200) {
return "Error!\ncUrl: $curl_error\nHTTP code: $http_code\nResult: $result";
} else {
return $result;
}