Send cookie with REST-call, please help!
I am developing a website that will be communicating with a REST-protocol. The owner of t开发者_如何学编程he 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!
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);
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);
?>
精彩评论