php - curl cookie 未成功创建

标签 php cookies curl

我正在使用 cUrl(PHP) 发布登录请求并将响应存储在 cookie 文件中。 在我的第二个请求中,我在 header 中传递 cookie 并发布数据以验证它。

问题是在第一次成功的请求中没有创建 cookie 文件导致第二次请求失败。请建议我哪里做错了。

$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);

最佳答案

我有同样的问题使用它在 windows 本地主机服务器上运行

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

关于php - curl cookie 未成功创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2537700/

相关文章:

php - 禁用 cookie 时 PHP session 如何工作?

internet-explorer - IE11 document.cookie 不创建 cookie

使用 cookie 的 JavaScript 登录

python - 将curl替换为相当于将文件POST到服务器的python请求

ssl - 自签名 SSL 证书中的颁发者证书无效

php 单 curl 有效但多 curl 不起作用?

php - 如何在 Facebook 应用程序中使用 jQuery

php - 从图像或扫描文档中提取表格数据(非 pdf)

php - Jquery UI 日历显示 mysql 日期?

php - URL 重写 OpenCart 产品 SEO