这是post方法请求网址的函数:
function send_post($url, $post_data) {
$postdata = http_build_query($post_data);
$options = array(
'http' => array(
'method' => 'get',
'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;
}
调用函数:
$post_data = array(
'username' => 'username',
'password' => 'password'
);
$html = $this->send_post('http://localhost:8980/newhotel/login/login',$post_data);
echo $html;
die;
请求的地址的方法中就写一句话:
echo $_POST;die;
但是浏览器上只输出了一个空array。。。。
这是为什么,get方法也是一样获取不到。。。。。到底怎样才能获取请求地址携带的数据呢?
哎,起初我看你这个代码,感觉除了坑爹的格式外,应该也没啥问题。
随手写了个PHP文件,一执行:
话说你调试的时候也不开display_errors吗?
那么原因就很简单了:
'method' => 'get',
这个地方指定HTTP METHOD的时候要使用大写的。比如
'method' => 'GET',
, 或者'method' => 'POST',
.修改后亲测有效。