tags:

views:

4063

answers:

4

So some guy at some other company thought it would be awesome if instead of using soap or xml-rpc or rest or any other reasonable communication protocol he just embedded all of his response as cookies in the header.

I need to pull these cookies out as hopefully an array from this curl response. If I have to waste a bunch of my life writing a parser for this I will be very unhappy.

Does anyone know how this can simply be done, preferably without writing anything to a file?

I will be very grateful if anyone can help me out with this.

A: 

If you use CURLOPT_COOKIE_FILE and CURLOPT_COOKIE_JAR curl will read/write the cookies from/to a file. You can, after curl is done with it, read and/or modify it however you want.

SoapBox
A: 

My understanding is that cookies from curl must be written out to a file (curl -c cookie_file). If you're running curl through PHP's exec or system functions (or anything in that family), you should be able to save the cookies to a file, then open the file and read them in.

kyle
He's almost certainly referring to php.net/curl :)
TML
+3  A: 
$ch = curl_init('http://www.google.com/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
preg_match('/^Set-Cookie: (.*?);/m', curl_exec($ch), $m);
var_dump(parse_url($m[1]));
TML
Unfortunately I have a feeling that this is the right answer. I think its ridiculous that curl can't just hand me a mapped array though.
thirsty93
I'll give it to you but the preg_match was wrong.I didn't just want the session, tho I understand why you would think that.But the genius who made their system is loading the cookie with an entire response map like with a get or post. Shit like this:Set-Cookie: price=1Set-Cookie: status=acceptI needed a preg_match_all with '/^Set-Cookie: (.*?)=(.*?)$/sm'
thirsty93
Not close enough to accept, then?
TML
No it is, I just haven't been on a computer in a bit :)
thirsty93
A: 

libcurl also provides CURLOPT_COOKIELIST which extracts all known cookies. All you need is to make sure the PHP/CURL binding can use it.

Daniel Stenberg