接口、接口测试类, get和post分别请求
来源:互联网 发布:java string长度 编辑:程序博客网 时间:2024/05/09 14:38
登录接口
<?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\n". "Content-Length:".strlen($data)."\r\n", '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; } }
0 0
- 接口、接口测试类, get和post分别请求
- 【接口测试】:HttpClient Post 和 Get 请求
- Linux 发送get和post请求测试接口
- CURL 请求外部接口 GET 和 POST
- web接口Flask测试之GET与POST请求
- web接口测试之GET与POST请求
- web接口测试之GET与POST请求
- python接口测试之路get、post、json请求处理
- PHP:CURL分别以GET、POST方式请求HTTPS协议接口api
- PHP:CURL分别以GET、POST方式请求HTTPS协议接口api
- java HttpUrlConnection get和post 模拟请求 调用接口
- curl请求接口的get和post方法
- post和get请求调用Http接口并拿到数据
- HttpClient测试接口的封装(get和post)
- java HttpClient get post请求 调用接口
- HTTP请求接口方法(POST/GET)
- Postman的GET,POST请求接口
- POST请求和GET请求分别解决乱码问题
- 深入了解MyBatis返回值
- APP IOS6 适配iOS7开发计巧
- ECMAScript5中数组迭代方法
- 前端菜鸟的成长学习之路(二)
- datable增加一列
- 接口、接口测试类, get和post分别请求
- UVA11889:Benefit(已知LCM和其中一个数,求另一个数)
- openstack云平台访问安全http->https
- Context.getExternalFilesDir()和Context.getExternalCacheDir()方法
- 简单的生成随机字符串的方法
- 大话数据结构—多路查找树(B树)
- ORA-06502: PL/SQL: 数字或值错误 : 字符串缓冲区太小
- Android apk 反编译
- kafka, storm,hdfs日志处理方法(附storm-kafka, storm-hdfs使用方法)