file_get_content 模拟POST数据
用file_get_content来get数据很简答,直接把URL参数放在URL中就可以了,但是POST呢,请看下面代码
文件1:index.php 用来检测是否有post数据。
<?php if($_POST['a'] && $_POST['b']) { echo 'post data success!'; exit(); }
文件2:indexx.php 主要测试文件。
<?php $url = 'http://www.test.com/index.php'; $data = array( 'a' => '1', 'b' => '2WWW' ); $params = array( 'http' => array( 'method' => 'POST', 'header' => "Content-type:application/x-www-form-urlencoded", 'content' => http_build_query($data), )); for($i=0;$i<3;$i++){ $response = file_get_contents($url, false, stream_context_create($params)); if ($response) { echo $response; break; } else { echo 'tring ' . $i . ' failed!<br />'; } }
得到结果:
post data success!
数据POST成功
来源:http://blog.csdn.net/fwkjdaghappy1/article/details/6226420