tags:

views:

194

answers:

3

Hi I'm using cUrl(PHP) to post a login request and store response in cookie file. In my second request I'm passing cookie in header and post data to verify it.

Issue is that cookie file is not created in first succesful request results in failure for second request. Please suggest me where I'm doing wrong.

$cookiefile="/var/www/html/dimdim/cook.txt";

$url_log="http://my.dimdim.com/api/auth/login"; $p_log='request={"account":"bin6k","password":"password","group":"all"}'; $url_ver="http://my.dimdim.com/api/auth/verify"; $p_ver='request={"account":"bin6k","password":"password","group":"all"}';

$ch = curl_init(); //curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);

curl_setopt($ch, CURLOPT_URL,$url_log); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $p_log);

ob_start(); // prevent any output $retval=curl_exec ($ch); // execute the curl command ob_end_clean(); // stop preventing output curl_close ($ch); //print_r($retval); unset($ch);

$ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile); curl_setopt($ch, CURLOPT_URL,$url_ver); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $p_log);

$buf2 = curl_exec ($ch);

curl_close ($ch);

echo "".htmlentities($buf2);

A: 

Try adding a curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile); in there as well.

Also, there's no need to start a fresh curl instance for each request. You can reuse the same instance for multiple requests. Just set the appropriate CURLOPT (url, postfields, get, etc...) each time and curl will sort things out internally.

Marc B
A: 
openprojdevel
A: 

i had the same problem use this for running in windows localhost server

curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__) . "/cookies.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . "/cookies.txt");
Ben