欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > php >内容正文

php

php无法创建cookie,php-curl cookie无法成功创建

发布时间:2023/12/20 php 32 豆豆
生活随笔 收集整理的这篇文章主要介绍了 php无法创建cookie,php-curl cookie无法成功创建 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

我正在使用cUrl(PHP)发布登录请求并将响应存储在cookie文件中.

在我的第二个请求中,我在标题中传递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 "

".htmlentities($buf2);

解决方法:

我在Windows本地服务器中运行时遇到了同样的问题

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

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

标签:cookies,curl,php

来源: https://codeday.me/bug/20191210/2099710.html

总结

以上是生活随笔为你收集整理的php无法创建cookie,php-curl cookie无法成功创建的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。