Reputation: 4901
I am using CURL for get response from an web api. I am getting response but it is in string format like this:
HTTP/1.1 302 Moved Temporarily Date: Fri, 16 Mar 2012 12:57:16 GMT Server: GlassFish/v3 X-Powered-By: Servlet/2.5 Location: http://demo.tdsarena.com/tds/ Content-Type: text/html; charset=iso-8859-1 Content-Length: 0 Set-Cookie: JSESSIONID=b869884w3w3r3r76dbd1a2bfd5d; Path=/tds Vary: Accept-Encoding Connection: close
i am using following code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://demo.tdsarena.com/tds/j_security_check');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "j_username=XXX&j_password=XXX");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
i want to get cookie "JSESSIONID". but how can i get this. I have tried with $_COOKIE variable.
can anybody help me plz. thanks in advance.
Upvotes: 2
Views: 5330
Reputation: 11556
You could parse the $result
value and extract the cookie value using something like this:
if (preg_match('/Set-Cookie: JSESSIONID=(.*?);/', $result, $matches))
{
$cookieVal = $matches[1];
echo $cookieVal;
}
Upvotes: 3