在 curl php 中发送第二个请求时如何保留会话?

how to retain session when sending second request in curl php?

我的问题是,当我第一次将数据从 PHP 同步到节点 js 时,我登录了 node.js 应用程序。但是当我在第二个请求上发送数据时登录后会话没有发送 curl 请求因此它显示 'not logged in'.

这是我的登录 curl 代码:

$data_string = json_encode(['userid' => $user_name, 'password' => $password]);
$URL = $url;     

$ch = curl_init('http://192.168.1.16:8000/auth/login');                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string))                                                                 
);

$result = curl_exec($ch);

并且正在发送数据卷曲代码:

$data_string = json_encode(['import_export' =>$json]);                                                                                   

https://whosebug.com/users
    $ch = curl_init();
    curl_setopt_array($ch, array(
        CURLOPT_URL => 'http://192.168.1.16:8000/api/all',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => $data_string,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTPHEADER => array(                                                                          
            'Content-Type: application/json',                                                                                
            'Content-Length: ' . strlen($data_string)
        )
    ));

    $result = curl_exec($ch);

告诉 cURL 使用 cookie:

curl_setopt(CURLOPT_COOKIEJAR, '/tmp/cookies.txt');
curl_setopt(CURLOPT_COOKIEFILE, '/tmp/cookies.txt');

这样默认的 Node/Express 基于会话 cookie 的身份验证应该可以工作,因为会话 cookie 将在登录时保存并随后续请求一起发送。

http://php.net/manual/en/function.curl-setopt.php