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);

相关推荐
MSTcheng.10 分钟前
【LinuxIO篇】Linux学习第二十三弹——Linux重定向到底改了什么?printf为何会写进文件,一文搞懂fd与dup2
linux·运维·服务器·操作系统·重定向·文件描述符
FungLeo14 分钟前
FengCMS · PHP+MySQL 升级实录· 依赖生命周期与来源核验:jQuery 4.0 迁移 + SHA-256 核验脚本
mysql·php·jquery·依赖管理·fengcms
kitsch0x9733 分钟前
MITRE Engage 中的网络欺骗
安全·web安全·php
FungLeo1 小时前
FengCMS · PHP+MySQL 升级实录 · CI 落地:PHP 8.4/8.5 矩阵、隔离凭证、诊断门槛失败
mysql·ci/cd·php·持续集成·fengcms
云起无垠16 小时前
关掉笔记本,让 AI agent 在远程服务器上继续跑完一夜:DevAgent 的 SSH 远程工作区实战
服务器·人工智能·ssh
无足鸟ICT18 小时前
【RHCA+】_proc_pid_task_
linux·运维·服务器
weixin_4046793119 小时前
cc 在 linux 安装
linux·运维·服务器
FungLeo21 小时前
FengCMS · PHP+MySQL 升级实录· 回归测试从 69 项到 478 项:HTTP/数据库回归套件怎么搭
数据库·mysql·php·回归测试·fengcms
贞明电子21 小时前
贞明电子网口设备 Modbus TCP 工业接入实战指南
服务器·网络协议·tcp/ip·贞明电子
小宋10211 天前
embedding 返回全零向量,索引却照收不误:入库前的向量健康门禁、隔离重算与召回恢复判据
linux·服务器·网络·人工智能