PHP CURL脚本运行,但不设置cookie

PHP CURL脚本运行,但不设置cookie,php,curl,cookies,Php,Curl,Cookies,我试图通过PHP CURL设置cookie超过24小时,但没有效果 之前,我一直在我的浏览器中设置cookie,将它们作为参数添加到url中,如下所示 http://localhost/setc.php?userid=123&panelid=1 但是现在我需要在运行scriptsetcookie.php时设置cookie 下面是我尝试过的各种类型代码的最新版本 setcookie.php $c = curl_init('http://localhost/atst.php?use

我试图通过PHP CURL设置cookie超过24小时,但没有效果

之前,我一直在我的浏览器中设置cookie,将它们作为参数添加到url中,如下所示

http://localhost/setc.php?userid=123&panelid=1
但是现在我需要在运行scriptsetcookie.php时设置cookie

下面是我尝试过的各种类型代码的最新版本

setcookie.php

$c = curl_init('http://localhost/atst.php?userid=628929&panelid=1');

curl_setopt($c, CURLOPT_VERBOSE, 1);
curl_setopt($c, CURLOPT_COOKIE, 'userid=123; panelid=1');
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
$page = curl_exec($c);
curl_close($c);
它仍然不能创建cookie,有人能帮忙吗


附言:如果你们也不明白这一点,至少给我一个提示/指导,告诉我如何设置一个简单的cookie而不出现任何复杂情况

只有当您使用curl\u close$ch关闭curl手柄时,cookiejar才会被保存

从手册中:

CURLOPT_COOKIEFILE包含cookie数据的文件的名称。cookie文件可以是Netscape格式,也可以只是简单的HTTP样式的头文件转储到文件中。如果名称为空字符串,则不会加载cookie,但cookie处理仍处于启用状态

CURLOPT_COOKIEJAR在句柄关闭时(例如在调用curl_close后)保存所有内部cookie的文件名


您是否错过或忘记添加$c=curl_init?我忘了它在原来的代码里了你有写权限到你试图保存文件的目录吗?谢谢你的描述顺便说一句。。。我应该用这里给出的$BASEURL替换我的url。是的,请检查这个答案为绿色。当然,伙计。。。但是我现在正在解决这个问题顺便说一句,你能不能在CURLOPT_COOKIEJARCURLOPT_COOKIEJAR用于在tmp文件夹中创建cookie名称之后,再检查一下这个/tmp/cookieFileName
$ckfile = tempnam ("/tmp/", "CURLCOOKIE");
        $BASEURL='http://localhost/openx/www/api/json/index.php/main/authenticate/';
        $POSTFIELDS='username='.$username.'&password='.$password.'';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/cookieFileName");
        curl_setopt($ch, CURLOPT_URL,'http://localhost/openx/www/api/json/index.php/main/authenticate/');
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $POSTFIELDS);

        ob_start();      // prevent any output
        $result=curl_exec ($ch); // execute the curl command
        ob_end_clean();  // stop preventing output

        $result = curl_exec($ch);
            curl_close($ch);