I access a REST api service that utilizes a variable called session_id. The API calls for this to be stored in a cookie and I accomplish this as follows:
$ch = curl_init(); // initialize curl handle
curl_setopt($ch, CURLOPT_URL, $url); //set target URL
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);// allow redirects
curl_setopt($ch, CURLOPT_COOKIEFILE, './Cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, './Cookie.txt');
curl_setopt($ch, CURLOPT_POST, TRUE); // set POST method
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); //set headers
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE); //return the headers so we can get session id
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, FALSE); //prevent unverified SSL certificate error
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE); //prevent unverified SSL ""
$contents = curl_exec($ch);
curl_close($ch);
Now the problem is this is called many different times by many different users. (so many different cookie files have to be saved) I'd like a way to simply store the session_id in a variable instead of referencing the cookie file. So far all my attempts are rejected by the service. Can anyone suggest methods to store the session id and cookie information without saving it to a file? Note that reading the session id is no problem, it's returned in XML. For some reason passing it back without referencing the actual generated file does not pass security credentials. Any more information as to why this might be would also be helpful.
Curl writes all cookies previously read from a specified file as well as all cookies received from remote server(s). If no cookies are known, no data will be written.
You can't. The only way this would work is if you use persistent cookies in your curl request. CURL can keep cookies itself. Assign a session ID to the cookie file (in curl) so subsequent requests get the same cookies.
Cookies are passed to Curl with the --cookie "Name=Value" command line parameter. Curl automatically converts the given parameter into the Cookie: Name=Value request header. Cookies can be sent by any HTTP method, including GET, POST, PUT, and DELETE, and with any data, including JSON, web forms, and file uploads.
We tell curl to store them to a file at /tmp/cookies using the -c switch. If you want to both send and store cookies, you need to supply both switches. You can optionally use the -j switch to tell curl to discard any cookies with "Session" expiry.
Cookies are simple text headers sent along with the request. CURL allows you to specify those directly using CURLOPT_COOKIE
.
curl_setopt($ch, CURLOPT_COOKIE, 'key=value;anotherkey=anothervalue');
If you know what information to send, you can construct your own cookie header this way. The COOKIEJAR/COOKIEFILE options just automate parsing, saving and sending. You'll have to do that manually (read received Cookie headers, create Cookie headers to be send), if you don't want to write to a file.
There is a good example here,
http://sgjoomla.googlecode.com/svn/trunk/joomla/curltest.php
For some reason, the cookie part is commented out but all the code you need is still there.
Basically, you parse the "Set-Cookie" headers yourself and save the cookie value by doing this,
curl_setopt ($ch, CURLOPT_HEADERFUNCTION, 'read_header');
Put the cookie value in a global. In next call, set it like this,
curl_setopt($ch, CURLOPT_COOKIE, "$cookieName=$cookieValue");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With