直接贴代码:
$data = array(
'username' => 'user',
'password' => 'pass'
);
$data = http_build_query($data);
$options = array(
'http'=>array(
'method'=> 'POST',
'header'=> "Content-type:application/x-www-form-urlencoded\r\n".
"Accept-language:en\r\n".
"Cookie:t=lon\r\n".
"Content-length:".strlen($data)."\r\n",
'content'=> $data,
'timeout'=> 5
)
);
$url = 'http://www.test.com/ci/blog/createUser';
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
var_dump($result);
说明:
stream_context_create()创建并返回一个资源流上下文,该资源流中包含了 options 中提前设定的所有参数的值。
$options可选项:
- method:远程服务器支持的get、post或者其他http方法,默认值是get
- header:请求期间发送的额外的header,在此选项值将覆盖其他值,详细可以查看http请求首部的键值对
- user_agent:要发送的 header User-Agent: 的值
- content:在header后边要发送的额外数据
- proxy:URI指定代理服务器地址
- timeout:读取超时时间,单位秒
- ignore_errors:即使是故障状态码依然获取内容,默认false
更多选项可查看PHP手册。