php模拟post 提交表单

时间:2021-04-26 09:32:38
<?php
header('Content-type: text/html;charset=UTF-8');
$action=$_REQUEST['action'];
//获取验证码
if($action=='ask_reg'){
$phone=$_REQUEST['phone'];
if(!preg_match('/^((13[0-9]{1})|159|153|189)+\d{8}$/',$phone)){
echo json_encode(array("result"=>"phoneError"));
exit;
}
$arr = array ("phone"=>$phone,"mac"=>"aabbccdreeff");
$data=json_encode($arr);
//$url='http://mobile.9797168.com:8080/uc/ask_reg?data={"phone":"15913160604","mac":"aabbccdreeff"}';
$url='http://mobile.9797168.com:8080/uc/ask_reg';
$json_data = postData($url, $data);
echo $json_data; }elseif($action=='usr_reg'){
//注册用户
$phone=$_REQUEST['phone'];
$nickname=$_REQUEST['nickname'];
$pswd=$_REQUEST['pswd'];
$validcode=$_REQUEST['validcode'];
if(!preg_match('/^((13[0-9]{1})|159|153|189)+\d{8}$/',$phone)){
echo json_encode(array("result"=>"phoneError"));
exit;
}
$arr = array (
"nickname"=>$nickname,
"phone"=>$phone,
"pswd"=>$pswd,
"mac"=>"aabbccdreeff",
"validcode"=>$validcode
);
$data=json_encode($arr);
$url='http://mobile.9797168.com:8080/uc/usr_reg';
$json_data = postData($url, $data); echo $json_data;
}elseif($action=='usr_login'){
//用户登陆
$phone=$_REQUEST['phone'];
$pswd=$_REQUEST['pswd'];
if(!preg_match('/^((13[0-9]{1})|159|153|189)+\d{8}$/',$phone)){
echo json_encode(array("result"=>"phoneError"));
exit;
}
$arr = array (
"phone"=>$phone,
"pswd"=>$pswd
);
$data=json_encode($arr);
$url='http://mobile.9797168.com:8080/uc/usr_login';
$json_data = postData($url, $data);
echo $json_data;
} //curl 模拟提交交
function postData($url, $data)
{
$ch = curl_init();
$timeout = 300;
curl_setopt($ch, CURLOPT_URL, $url);
//curl_setopt($ch, CURLOPT_REFERER, "http://127.0.0.1/"); //构造来路
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'data='.$data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$handles = curl_exec($ch);
curl_close($ch);
return $handles;
}