tags:

views:

27

answers:

2

Hello!

I am developing a website that will be communicating with a REST-protocol. The owner of the REST service wants a cookie to be sent along with the REST call, perhaps via header.

How is this done in PHP, how can I send a cookie along with a REST-call?

Thankful for all help!

A: 

You can modify headers using cURL functions.

<?php
$submit_url = "https://sitename/process.php";

$curl = curl_init();

curl_setopt($curl, CURLOPT_COOKIEFILE, $cookiefile);
curl_setopt($curl, CURLOPT_URL, $submit_url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params );
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");

$data = curl_exec($curl);
curl_close($curl);
?>
Scorpil
The cookie was set with a http set-cookie command by the owner of the API after I authorized myself via Facebook Connect apparently. How can I get it to populate the $cookiefile.
A: 

If you're using cURL, take a look at curl_setopt options CURLOPT_COOKIEJAR (storing cookies from a response) and CURLOPT_COOKIEFILE (loading cookies before request). It should be sufficient to set both to the same file.

$yourfile = '/any/file/you/want';
$ch = curl_init(); 
curl_setopt ($ch, CURLOPT_URL, $url); 
curl_setopt ($ch, CURLOPT_POST, true); 
curl_setopt ($ch, CURLOPT_COOKIEFILE, $yourfile); 
curl_setopt ($ch, CURLOPT_COOKIEJAR, $yourfile); 
$result = curl_exec ($ch); 
curl_close ($ch); 
Piskvor
So how would a POST request with PHP and CURL with a cookie sen along look like?
@user339067: added an example
Piskvor
(just as an aside, if it has cookies, it's not *pure* REST, but that's more of a religious issue than anything else)
Piskvor