最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
PHP发送POST请求的常用方式
时间:2022-06-24 16:46:46 编辑:袖梨 来源:一聚教程网
在PHP开发的过程中经常需要发送POST请求,POST相比GET要安全很多,而且传输的数据量也较大。下面PHP程序员雷雪松就带大家一起总结下PHP发送POST请求的几种常用方式,分别使用curl、file_get_content来实现POST请求和传递参数。
1、curl实现PHP POST请求和传递参数。
$data=array("username"=>"raykaeso","name"=>"雷雪松");//post参数
$url="https://www.111cn.net";
$ch = curl_init();//创建连接
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));//将数组转换为URL请求字符串,否则有些时候可能服务端接收不到参数
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1); //接收服务端范围的html代码而不是直接浏览器输出
curl_setopt($ch, CURLOPT_HEADER, false);
$responds = curl_exec($ch);//接受响应
curl_close($ch);//关闭连接
2、file_get_content实现PHP POST请求和传递参数
$data=array("username"=>"raykaeso","name"=>"雷雪松");//post参数
$url="https://www.111cn.net";
$content = http_build_query($data);
$length = strlen($content);
$options = array(
'http' => array(
'method' => 'POST',
'header' =>
"Content-type: application/x-www-form-urlencodedrn" .
"Content-length: $length rn",
'content' => $content
)
);
file_get_contents($url, false, stream_context_create($options));
相关文章
- onedrive网页版登录入口-onedrive官网登录入口 03-13
- 腾讯视频24小时人工客服电话专线-腾讯视频人工客服网点详情 03-13
- QQ邮箱网页版快捷登录-QQ邮箱官网网页版直达入口 03-13
- 夸克官方网页版直达-夸克浏览器极速入口 03-13
- 刀剑乱舞网页版官网-刀剑乱舞h5游戏入口 03-13
- 漫蛙漫画网manwa.me-漫蛙ManWa官网最新地址2 03-13