Wednesday, March 31, 2010

Sending Cookies with cURL and PHP

For some reason, this was something I haven’t had to do before and was surprisingly hard to find a resource for. This comes in handy when dealing with an XML API, which is what I was using it for. In my case, I had to authenticate by pulling a cookie from a server and sending it back with every subsequent request.

Example:

$c = curl_init(‘PATH_YOU_ARE_SENDING_TO’);
curl_setopt ($c, CURLOPT_COOKIE, $cookie);
curl_setopt ($c, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec ($c);
curl_close ($c); //close connection

This will return the response from the server and store it in $page. Very simple.