php使用file_get_contents提交post请求

来源:赵克立博客 分类: PHP 标签:PHP发布时间:2014-10-07 23:10:00最后更新:2018-01-10 09:03:37浏览:1163
版权声明:
本文为博主原创文章,转载请声明原文链接...谢谢。o_0。
更新时间:
2018-01-10 09:03:37
温馨提示:
学无止境,技术类文章有它的时效性,请留意文章更新时间,如发现内容有误请留言指出,防止别人"踩坑",我会及时更新文章

file_get_contents版本:

可以添加请求header头信息


/**
 * post请求数据
 * @param  [type] $url       url地址
 * @param  [type] $post_data post的数据数组key=>value对
 * @return [type]            返回网页内容
 */
function send_post($url, $post_data) {
	$header = <<<eot
Content-type:application/x-www-form-urlencoded
Host:www.xxx.com
Referer:http://www.xxx.com/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36
X-Requested-With:XMLHttpRequest
eot;
	$postdata = http_build_query($post_data);
	$options  = array(
		'http' => array(
			'method'  => 'POST',
			'header'  => $header,
			'content' => $postdata,
			'timeout' => 15 * 60, // 超时时间(单位:s)
		),
	);
	$context = stream_context_create($options);
	$result  = file_get_contents($url, false, $context);
	return $result;
}
//使用方法
$post_data = array(
	'username' => 'stclair2201',
	'password' => 'handan'
);
send_post('http://blog.snsgou.com', $post_data);



微信号:kelicom QQ群:215861553 紧急求助须知
Win32/PHP/JS/Android/Python