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

相关推荐
鬼手点金7 小时前
nanoGPT/nanochat 能做出 DeepSeek/Qwen 这样的大模型吗?
服务器·前端·javascript·bug·openclaw
贵州诚鑫致达科技有限公司8 小时前
Linux的yum源怎么重启?源不是服务
linux·运维·服务器
遇印记9 小时前
传输层,网络层
运维·服务器·网络·windows·学习
Elastic 中国社区官方博客9 小时前
一条 ES|QL 查询,涵盖365天的审计日志:在热数据和可搜索快照上执行相同的搜索
大数据·运维·服务器·elasticsearch·搜索引擎·全文检索
北京中科新远科技10 小时前
高速光模块上线验收实战:DOM、FEC、BER与端口计数器怎么一起看
服务器·网络·php
澜山奕秋11 小时前
RHCSA
linux·运维·服务器
pride.li11 小时前
第03章 Python 教程-基础语法:注释与变量
服务器·前端·后端·python·算法·计算机视觉
.Bryce.12 小时前
在智能体中使用A2A工具:让大模型自己决定“该找谁帮忙“
android·java·服务器·redis·后端·agent·a2a
对空六课12 小时前
指标口径生效时间的版本查询实现
大数据·服务器·前端·算法·数据分析
对空六课12 小时前
RFM分层边界变化的回溯计算
大数据·服务器·前端·数据库·人工智能·算法·数据分析