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

相关推荐
m0_459872924 小时前
Linux 系统基础
linux·运维·服务器
ID34610744204 小时前
【课程设计】基于Spring Boot+Vue的校园共享无人机服务系统设计与实现-计算机毕设 附源码44219
javascript·vue.js·spring boot·python·node.js·php·课程设计
2401_891957315 小时前
简单了解多路转接select
运维·服务器
程序员-Benothing6 小时前
Shell 脚本条件判断与流程控制:if for while case 详解
linux·运维·服务器
captain3766 小时前
网络原理(3)-TCP核心机制连接管理▲▲▲
java·服务器·网络·ide·网络协议·tcp/ip·java-ee
君生我老6 小时前
Linux动静态库
linux·服务器
troy1286 小时前
Python 基础语法(一):变量、数据类型与运算符
java·服务器·python
sbjdhjd6 小时前
ThinkPHP 5.0.10 缓存写入型 RCE 复盘:从手动搭建、换行绕过到源码单步验证 | 05
java·后端·安全·spring·网络安全·数据挖掘·php
程序员清风6 小时前
记忆系统设计:让 Agent 拥有短期记忆与长期记忆
java·服务器·网络
en.en..7 小时前
TCP/UDP 收发流程(网络字节序---函数讲解)
开发语言·php