我想用 curl 为我的网站创建一个登录名来管理一些东西。因此我必须使用相同的 cookie 发出多个 curl 请求
现在我想知道什么代码更好地实现这一目标。这是否更好:
$CookieFile = 'cookies/'. uniqid() . '.txt';
file_put_contents($CookieFile, '');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $PostData1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $CookieFile);
curl_setopt($ch, CURLOPT_COOKIEJAR, $CookieFile);
$result1 = curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $PostData2);
$result2 = curl_exec($ch);
curl_close($ch);
还是这样做更好
$CookieFile = 'cookies/'. uniqid() . '.txt';
file_put_contents($CookieFile, '');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $PostData1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $CookieFile);
curl_setopt($ch, CURLOPT_COOKIEJAR, $CookieFile);
$result1 = curl_exec($ch);
curl_close($ch);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $PostData2);
curl_setopt($ch, CURLOPT_COOKIEFILE, $CookieFile);
curl_setopt($ch, CURLOPT_COOKIEJAR, $CookieFile);
$result2 = curl_exec($ch);
curl_close($ch);
我不太确定哪个版本更好,我有点担心饼干。或者有没有我没想到的更好的版本?