如何使用php中的curl方法向服务器发送post请求?
用PHP向服务器发送HTTP的POST请求,代码如下:
<?php/** * 发送post请求 * @param string $url 请求地址 * @param array $post_data post键值对数据 * @return string */ function send_post($url, $post_data) { $postdata = http_build_query($post_data); $options = array( 'http' => array( 'method' => 'POST', 'header' => 'Content-type:application/x-www-form-urlencoded', 'content' => $postdata, 'timeout' => 15 * 60 // 超时时间(单位:s) ) ); $context = stream_context_create($options); $result = file_get_contents($url, false, $context); return $result; }
postdata是什么意思?
方法1、最常见的方法是:$_post['fieldname'];
说明:只能接收content-type:application/x-www-form-urlencoded提交的数据
解释:也就是表单post过来的数据
方法2、file_get_contents("php://input");
说明:
允许读取post的原始数据。
和$http_raw_post_data比起来,它给内存带来的压力较小,并且不需要任何特殊的php.ini设置。
php://input不能用于enctype="multipart/form-data"。
解释:
对于未指定content-type的post数据,则可以使用file_get_contents(“php://input”);来获取原始数据。
事实上,用php接收post的任何数据都可以使用本方法。而不用考虑content-type,包括二进制文件流也可以。
所以用方法二是最保险的方法
方法3、$globals['http_raw_post_data'];
说明:
总是产生$http_raw_post_data变量包含有原始的post数据。
此变量仅在碰到未识别mime类型的数据时产生。
$http_raw_post_data对于enctype="multipart/form-data"表单数据不可用
如果post过来的数据不是php能够识别的,可以用$globals['http_raw_post_data']来接收,
还没有评论,来说两句吧...