您好,欢迎访问一九零五行业门户网

PHP发送POST请求的常用方式

分类:php时间: 2016年6月6日
在php开发的过程中经常需要发送post请求,post相比get要安全很多,而且传输的数据量也较大。下面php程序员雷雪松就带大家一起总结下php发送post请求的几种常用方式,分别使用curl、file_get_content来实现post请求和传递参数。
1、curl实现php post请求和传递参数。
$data=array(username=>raykaeso,name=>雷雪松);//post参数 $url=http://www.leixuesong.cn; $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=http://www.leixuesong.cn; $content = http_build_query($data); $length = strlen($content); $options = array( 'http' => array( 'method' => 'post', 'header' => content-type: application/x-www-form-urlencoded\r\n . content-length: $length \r\n, 'content' => $content ) ); file_get_contents($url, false, stream_context_create($options));
其它类似信息

推荐信息