FortiGate 200B-POE API Poll (PHP Preserving Cookies)

I have been helping Ivan with trying to preserve cookies while polling the web service for a json response of the router’s status. As there are not much tutorials or sample codes provided by the vendor, I hope the code below explains how it can be done in PHP.

array(
'timeout' => 25,
'method'=>"POST",
'header'=>"Accept-language: en\r\n"
)
);
$context = stream_context_create($opts);
$xm = file_get_contents($request, false, $context);

$johncookie = $http_response_header[2];
$john = explode("APSCOOKIE_2464548051=", $johncookie);

$req = "https://192.168.1.200/api/monitor?json={'vdom':'root','path':'router','name':'ipv4','action':'select','start':'0','count':'1000'}";
$opts = array(
'http'=>array(
'timeout' => 25,
'method'=>"POST",
'header'=>array(
"Cookie: APSCOOKIE_2464548051=" . $john[1] . "\r\n"
)
)
);
$context = stream_context_create($opts);
$xml = file_get_contents($req, false, $context);

echo htmlspecialchars($xml, ENT_QUOTES);

?>

Hope it helps some of you 🙂