我想通过 curl 登录并维护 cookie 和会话信息以供进一步调用。我在同一目录中创建了 cookie 文本文件并使用 CURLOPT_COOKIEJAR 、CURLOPT_COOKIEFILE 来维护 CUL 中的 cookie。每当我尝试调用登录 api 时,它都会使用旧 cookie 并显示以前的用户信息。我需要维护不同的用户 cookie 并像普通浏览器句柄一样维护会话。怎么做。任何人都给出想法去做。
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HEADER,0); // TRUE to include the header in the output.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // it will follow with server redirects
curl_setopt($ch,CURLOPT_AUTOREFERER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//ssl certificate verifyer
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); //ssl certificate host
// Set the location of and send the cookies
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . "/cookies.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__) . "/cookies.txt");
curl_setopt( $ch, CURLOPT_COOKIESESSION, true );
$result = curl_exec($ch); //execute curl and store data in result