1

I need to use cURL to get data from a website but I need to include the session token in my request header. The problem is, the session token is stored in the browser's local storage. Since cURL is not a web browser, how can I get the session token?

EDIT: Here's my code:

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;
}
Esthon Wood
  • 315
  • 3
  • 19
  • Please provide some code, the question itself is way too broad – Kevin Kopf Jan 15 '16 at 01:51
  • @Nordenheim please see my edit. When I visit 'https://website.com' in a browser, it stores the session token in the browser's local storage. I need to get the session token so I can pass it on my next cURL request. – Esthon Wood Jan 15 '16 at 02:02
  • Is this process triggered by a browser, or is it completely unattended? – msanford Jan 15 '16 at 02:21
  • @msanford it is completely unattended with no user interaction. It would have been easier if the website stores their session token in the cookie but instead, they store it in the browser's local storage. – Esthon Wood Jan 15 '16 at 02:24
  • 1
    You can't have access to the localstorage, it's client only. – Eric Jan 15 '16 at 03:00
  • @Eric yes, I figured. I was thinking if other developers have some sort of a workaround on this. – Esthon Wood Jan 15 '16 at 03:05

0 Answers0