file_get_contents模拟表单(POST/GET方式提交)

$name         = 'yangheping';
$pwd          = 'yhpadmin';
$phone        = '158********';
$content      = '测试POST\GET发送方式';

//组合提交URL以及参数 (GET方式)
//$http       = 'http://www.ceshi.cc/php/post.php'.'?name='.$name.'&pwd='.$pwd.'&dest='.$phone.'&content='.$content;
//$result     = get_to($http);


//组合提交URL以及参数 (POST方式)
$http       = 'http://www.ceshi.cc/php/post.php';
$var        = 'name='.$name.'&pwd='.$pwd.'&dest='.$phone.'&content='.$content;

$result = post_to($var, $http);

/** * 提交的GET接口 * @param string $http 接口地址 * @return int */ /* function get_to($http) { $context = array( 'http'=>array( 'timeout'=> 20, 'method' => 'GET', 'header' => "Content-type: application/x-www-form-urlencoded\r\n", ) ); $xcontext = stream_context_create($context); $ret = @file_get_contents($http, false, $xcontext); //返回 return $ret; } */ /** * 提交的post接口 * @param array $vars 数据 * @param string $http 接口地址 * @return int */ function post_to($vars, $http){ $context = array( 'http'=>array( 'timeout'=> 20, 'method' => 'POST', 'header' => "Content-type: application/x-www-form-urlencoded\r\n" . "Content-length: " . strlen($vars)."\r\n", 'content'=> $vars ) ); $xcontext = stream_context_create($context); $ret = @file_get_contents($http,false,$xcontext); //返回 return $ret; }
posted @ 2012-08-29 15:43  WhoAmMe  阅读(622)  评论(0编辑  收藏  举报