インタフェース、インタフェーステストクラス、getとpostはそれぞれ要求します
4346 ワード
ログインインタフェース
インタフェーステスト(呼び出しとも呼ばれる)
============================
例:
<?php
class JsonUserAction extends action{
public function login(){
$phone=$_POST['phone'];
//$phone = '153';
if(empty($phone)){
$result=array('err_no' =>1001,'err_msg'=>"unauthorized request"); //
echo json_encode($result); exit;
}else{
$result=array('err_no' =>0,'err_msg'=>"success"); //
echo json_encode($result); exit;
}
}
}
?>
インタフェーステスト(呼び出しとも呼ばれる)
<?php
class ApiTestAction extends action{
public function apitest(){
//get
/*$phone = '153';
$url = 'http://localhost/index.php/JsonUser/login';
//
$opts = array(
'http'=>array(
'method'=>'POST',
//'timeout'=>15,
)
);
//
$context = @stream_context_create($opts);
$result = @file_get_contents($url,false,$context);
$obj = json_decode($result,TRUE); //decod json ,
var_dump($obj);
echo $obj['err_msg'];
if(isset($obj) && $obj['err_no']=0) // ,
echo 'OK';
else
echo 'FALSE';*/
$data=array('phone'=>'153', //
);
//echo date('y-m-d h:i:s',time());
$data=http_build_query($data);
$opts=array(
'http'=>array(
'method'=>'POST',
'header'=>"Content-type:application/x-www-form-urlencoded\r
".
"Content-Length:".strlen($data)."\r
",
'content'=>$data
),
);
$context=stream_context_create($opts);
$html = file_get_contents('http://localhost/index.php/JsonUser/login',false,$context);//http
echo $html;
$obj = json_decode($html,TRUE); //decod json ,
var_dump($obj);
}
}
?>
============================
例:
//
public function login(){
$phone = $_POST['phone'];
$password = $_POST['password'];
//$phone = '153';
if(empty($phone)){
$result=array('err_no' =>1001,'err_msg'=>"login name is empty"); //
echo json_encode($result); exit;
}
if(empty($password)){
$result=array('err_no' =>1002,'err_msg'=>"password id empty"); //
echo json_encode($result); exit;
}
if(!preg_match("/^1[34578]\d{9}$/", $phone)){
$result=array('err_no' =>1003,'err_msg'=>"login name isnot phone number"); //
echo json_encode($result); exit;
}
if(strlen($password)<6 or strlen($password)>16 ){
$result=array('err_no' =>1004,'err_msg'=>"password is wrongful"); // 6-16
echo json_encode($result); exit;
}
//die();
$User = M('user');
$result = $User->where("phone= "."'$phone'".' AND password= '." '$password' ")->find();
if($result){
$result=array('err_no' =>1,'err_msg'=>"success"); //
echo json_encode($result); exit;
}else{
$result=array('err_no' =>0,'err_msg'=>"failure"); //
echo json_encode($result); exit;
}
}