Curl cookie does not create success

I use cUrl (PHP) to send a login request and store the response in a cookie. In my second request, I pass a cookie in the header and send data to check it.

The problem is that the cookie is not created on the first successful request, which causes the second request to fail. Please suggest me where I am 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 "<PRE>".htmlentities($buf2);
+3
source share
3 answers

I had the same problem as for working in a local windows server

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

Try adding there curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);.

, curl . . CURLOPT (url, postfields, get ..) , curl .

0

Try this function, the cookijar option has been added:

function execute($toLoad) {

    if ( !preg_match( '/^http/', $toLoad ) ) {
        $toLoad = 'http://'.$toLoad;
    }
    $cookiefile = APP_PATH.'tmp/cookie.txt';
    $data = array();    
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,10);
    curl_setopt($ch, CURLOPT_TIMEOUT,10);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_USERAGENT,"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.6) Gecko/20091216 Fedora/3.5.6-1.fc11 Firefox/3.5.6 GTB6");
    curl_setopt($ch, CURLOPT_URL, $toLoad); // The page to download
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_REFERER, 'http://somesite.com/' ); 
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile); 
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);    

    $data['data'] = curl_exec($ch);
    $data['status'] = curl_getinfo($ch);

    //$this->out(curl_error($ch));   
    //$this->out(curl_getinfo($ch));     
    //$this->out('');
    //$this->out($data);
    //$this->out('');

    curl_close($ch);

    return $data;
}

you can also try reading the header of the returned data for Set-Cookie: SOMEKEY

and then in curlopt use

if ($header) {
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Cookie:'.$header)); 
} 

thank

0
source

Source: https://habr.com/ru/post/1738951/


All Articles