您需要使用cookiejar参数在两个请求之间共享文本文件.
/* Create a temporary cookie file */
$ckfile = tempnam ("/tmp", "COOKIES");
/* Visit the first page */
$ch = curl_init ("http://somedomain.com/");
curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch);
/* Visit the second page */
$ch = curl_init ("http://somedomain.com/cookiepage.php");
curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch);
Run Code Online (Sandbox Code Playgroud)
从http://coderscult.com/php-curl-cookies-example/修改的示例
编辑:我应该已经阅读了tempnam的文档...它创建了一个唯一的文件名,因此除非您希望以后能够识别该文件,否则不需要添加额外的用户特定前缀.您还可以查看调用fclose时自动删除的tmpfile()(通常在php请求结束时).