为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
一只萌萌小番薯 2023-03-04 18:05:05
我正在尝试将第 3 部分 API 集成到 PHP curl 中。它工作正常并且在我尝试时得到了响应postman。但它在我的 PHP CURL 代码中不起作用。我发现邮递员的标头中设置了 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);// Instruct cURL to store cookies in this file.curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');// Instruct cURL to read this file when asked about cookies.curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');$response1 = curl_exec($ch);curl_close($ch);$res = json_decode($response1,true);
查看完整描述

1 回答

?
ABOUTYOU

TA贡献1812条经验 获得超5个赞

您正在访问的 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');

JSESSIONID听起来像“会话 ID”cookie,用于跟踪会话或一系列请求的东西。会话 ID2cba2d9d7dc5455b76t90f4ccac3与您使用 Postman 启动的会话相关联,您需要将其从 PHP 请求中删除。


查看完整回答
反对 回复 2023-03-04
  • 1 回答
  • 0 关注
  • 185 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信