亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 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 關注
  • 301 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號