php写一个简单的接口
dearweb 发布:2021-11-05 09:19:59阅读:客户端代码
<!-- login.php --> <html > <head > <meta charset="utf-8"/> <title >My Web</title> </head> <!---------------------------------------> <body > <!-- POST 请求 --> <form method="post" action="http://localhost/post.php"> <!-- GET 请求 --> <!-- <form method="get" action="http://localhost/get.php"> --> <p align="center">用户名:<input type="text" name="username"> </p> <br/> <p align="center">密 码:<input type="text" name="password"> </p> <br/> <p align="center"><input type="submit" value="登录"/></p> </form> </body> </html>
get请求返回数据
http://localhost/get.php?username=zhangwei&password=123456
<?php header("Content-type:text/html; charset=utf-8"); // 获取form表单值 $username = $_GET['username']; $password = $_GET['password']; // 判断form表单中key if(isset($_GET['username']) && isset($_GET['password'])){ // 判断username和password if($username == "zhangwei" && $password == "123456"){ $result = array("success" => 1, "code" => 101, "data" => array("username" => $username, "password" => $password)); }else{ $result = array("success" => 0, "code" => 103, "data" => null); } }else{ $result = array("success" => 0, "code" => 100, "data" => null); } // 将错误信息(数组)转换成json类型,返回前端 echo(json_encode($result)); ?>
post请求返回数据
http://localhost/post.php
<?php header("Content-type:text/html; charset=utf-8"); // 获取form表单值 $username = $_POST['username']; $password = $_POST['password']; // 判断form表单中key if(isset($_POST['username']) && isset($_POST['password'])){ // 判断username和password if($username == "zhangwei" && $password == "123456"){ $result = array("success" => 1, "code" => 101, "data" => array("username" => $username, "password" => $password)); }else{ $result = array("success" => 0, "code" => 103, "data" => null); } }else{ $result = array("success" => 0, "code" => 100, "data" => null); } // 将错误信息(数组)转换成json类型,返回前端 echo(json_encode($result)); ?>
小礼物走一波,支持作者
赏还没有人赞赏,支持一波吧