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

问题描述 投票:0回答:1

我的问题是,当我第一次将数据从PHP同步到节点js时,我登录到node.js应用程序。但是在我第二次请求发送数据后登录时,会话没有发送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);

它正在发送数据Curl代码:

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

https://stackoverflow.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);
php node.js session curl session-cookies
1个回答
1
投票

告诉cURL使用cookies:

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

© www.soinside.com 2019 - 2024. All rights reserved.