php下curl发送cookie

目录

[一:使用 CURLOPT_COOKIE 选项](#一:使用 CURLOPT_COOKIE 选项)

二:CURLOPT_COOKIEFILE

三:CURLOPT_HTTPHEADER


php curl发送cookie的几种方式,下面来介绍下

通过设置 CURLOPT_COOKIE 选项,你可以将 cookie 字符串传递给 cURL 请求

$url = 'http://127.0.0.1/a/b/c';

$cookie = "name=value; another_name=another_value";

$headerArray =array("Content-type:application/json;charset='utf-8'",

"Accept:application/json","Platform:pc");

$ch = curl_init();

curl_setopt(ch, CURLOPT_URL, url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt(ch, CURLOPT_HTTPHEADER, headerArray);

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS, []);

curl_setopt(ch, CURLOPT_COOKIE, cookie);

response = curl_exec(ch);

curl_close($ch);

cookie信息如下:

{"connection":"close","content-type":"application\/json;charset='utf-8'","content-length":"0","platform":"pc","accept":"application\/json","cookie":"name=value; another_name=another_value","host":"127.0.0.1"}

二:CURLOPT_COOKIEFILE

使用 CURLOPT_COOKIEFILE ,通过设置 CURLOPT_COOKIEFILE 选项,你可以指定一个包含 cookie 的文件,cURL 将从该文件中读取 cookie。

$url = 'http://127.0.0.1/a/b/c';

$cookiePath = 'E:/DB/cookie.txt';

$headerArray =array("Content-type:application/json;charset='utf-8'",

"Accept:application/json","Platform:pc","Cookie: name=value; another_name=another_value");

$ch = curl_init();

curl_setopt(ch, CURLOPT_URL, url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt(ch, CURLOPT_HTTPHEADER, headerArray);

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS, []);

curl_setopt(ch, CURLOPT_COOKIEFILE, cookiePath);

response = curl_exec(ch);

curl_close($ch);

三:CURLOPT_HTTPHEADER

使用 CURLOPT_HTTPHEADER 选项:你可以使用 CURLOPT_HTTPHEADER 选项来发送自定义的 HTTP 头信息,包括 Set-Cookie 头。例如:

$url = 'http://127.0.0.1/a/b/c';

$headerArray =array("Content-type:application/json;charset='utf-8'",

"Accept:application/json","Platform:pc","Cookie: name=value; another_name=another_value");

$ch = curl_init();

curl_setopt(ch, CURLOPT_URL, url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt(ch, CURLOPT_HTTPHEADER, headerArray);

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS, []);

response = curl_exec(ch);

curl_close($ch);

相关推荐
wheeldown几秒前
【Linux】Linux进程间通信:命名管道(FIFO)的模拟实现重要知识点梳理
linux·运维·服务器
Crazy________4 分钟前
34部署LNMP架构详细解析
linux·运维·服务器·nginx
notillusion24 分钟前
KWW#71843
java·php·程序优化
notillusion1 小时前
TRX#22597
java·php·程序优化
ONE_SIX_MIX2 小时前
Debian 的 网络管理器 被意外卸载,修复过程
服务器·网络·debian
板鸭〈小号〉2 小时前
connect 的断线重连
运维·服务器
、花无将2 小时前
PHP:下载、安装、配置,与apache搭建
android·php·apache
wydaicls2 小时前
Linux 内核伙伴系统在快速路径分配内存时,对一个内存区域(Zone)进行水位线检查和内存压力评估的关键逻辑
linux·服务器
黄昏晓x3 小时前
Linux----权限
linux·运维·服务器
小白不想白a3 小时前
【shell】每日shell练习(系统服务状态监控/系统性能瓶颈分析)
linux·运维·服务器