如何在php curl的cookie中设置JSESSIONID?

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

我正在尝试将第三部分API集成到PHP curl中。

当我在postman中尝试时,它工作正常并且得到了响应。但是它不能在我的PHP CURL代码中使用。

enter image description here我发现邮递员的标头中设置了JSESSIONID。我不知道如何在php curl中创建该JSESSIONID并在Cookie中进行设置。

有人知道吗?

PHP代码

$url = "http://website.com/Public/login/";
$dataa["userNo"] = "username";
$dataa["userPassword"] = "password";

$data = json_encode($dataa);

$ch = curl_init($url);

$headers = array(
        'Content-type: application/json',
        "Cookie: JSESSIONID=2cba2d9d7dc5455b76t90f4ccac3; httpOnly"
    );


curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch,CURLOPT_POSTFIELDS,$data);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt( $ch, CURLOPT_COOKIESESSION, TRUE);

$response1 = curl_exec($ch);

curl_close($ch);
$res = json_decode($response1,true);
php curl jsessionid
1个回答
0
投票

您正在访问的URL试图设置cookie。

要接受带有cURL的cookie,您需要指定一个“ cookie jar”,即cURL可以在其中存储从响应中接收到的cookie的文件:

// Instruct cURL to store cookies it receives in this file.
curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookies.txt');

// Instruct cURL to read this file when asked about cookies.
curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookies.txt');
© www.soinside.com 2019 - 2024. All rights reserved.