php下使用curl进行多种数据编码方式的POST请求
来源:互联网 发布:淘宝订单编号生成规则 编辑:程序博客网 时间:2024/06/07 08:01
php使用curl请求数据是很常见的,但是根据HTTP/1.1 协议下的POST提交数据编码方式的不同,使用curl函数参数的选择也是有所区别的。
请求报文头header中的 Content-Type标记着传输的编码方式供服务端识别,以下根据Content-Type的不同正确使用curl传输数据
一.application/x-www-form-urlencoded方式:
1.普通类似web表单数据:
curl方法:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
处理示例:
请求:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
接收处理:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
test.txt文件内容是: test1, 11, http://www.test.com, #EEEEEE, s:5:”Array”;,
可以看出 提交的数组无法正确接收
如果要传数组,则要将curl_post方法中的
- 1
改为
- 1
2.传json,沿用上面的curl_post方法不变,只是将json内容当做字符串内容传递,要有个头部字段名与之对应,方面接收
处理示例:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
接收处理:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
test.txt文件内容是:
a:5:{s:6:”touser”;s:3:”test1”;s:11:”template_id”;s:2:”11”;s:3:”url”;s:19:”http://www.test.com“;s:8:”topcolor”;s:7:”#EEEEEE”;s:4:”data”;a:3:{i:0;s:2:”t1”;i:1;s:2:”t2”;i:2;s:2:”t3”;}}
可以看出是数组$template的内容
二.直接application/json方式:
curl设置header将Content-Type改为application/json
curl方法
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
请求:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
接收处理:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
test.txt文件内容是:
s:110:”{“touser”:”test1”,”template_id”:”11”,”url”:”http:\/\/www.test.com”,”topcolor”:”#EEEEEE”,”data”:[“t1”,”t2”,”t3”]}”;
- php下使用curl进行多种数据编码方式的POST请求
- php下使用curl进行多种数据编码方式的POST请求
- php下使用curl进行多种数据编码方式的POST请求
- PHP发送POST请求的三种方式 分别使用curl file_get_content fsocket 来实现post提交数据
- PHP发送POST请求的三种方式 分别使用curl file_get_content fsocket 来实现post提交数据
- PHP下使用CURL方式POST数据至API接口的方法
- PHP下使用CURL方式POST数据至API接口的代码
- PHP使用CURL模拟POST/GET方式提交数据,支持HTTPS请求
- 使用PHP CURL的POST数据
- 使用PHP CURL的POST数据
- 使用PHP CURL的POST数据
- php curl模拟post请求提交数据
- php使用curl发送post请求
- PHP使用CURL进行POST操作时
- php中利用curl进行post请求示例
- php 通过curl进行GET/POST(包含鉴权)请求
- PHP 模拟CURL分别GET POST进行HTTP请求
- php 使用 curl 发送 post 数据
- STL算法总结
- 安卓有关数据共享的8个问题回答笔记
- mac环境使用pip添加python组件权限不足
- Luogu 2679(dp)(NOIP 2015)
- 停课记录
- php下使用curl进行多种数据编码方式的POST请求
- 看懂正则表达式入门
- 剑指offer之Python练习一
- angularjs之猜字游戏
- PICRUSt:预测宏基因组功能—16S扩增子分析锦上添花
- 学习快速排序
- 简单实用的OkHttp
- sqlserver中分区函数 partition by的用法
- UNIX--文件I/O函数--open、creat、close、lseek、read、write等各种函数 详解