">
当前位置:   article > 正文

PHP 开发API接口 注册,登录,查询用户资料_apiid怎么查看

apiid怎么查看

服务端

  1. <?php
  2. require 'conn.php';
  3. header('Content-Type:text/html;charset=utf-8');
  4. $action = $_GET['action'];
  5. switch ($action) {
  6. //注册会员
  7. case"adduserinfo";
  8. $username = lib_replace_end_tag(trim($_GET['username']));
  9. $password2 = lib_replace_end_tag(trim($_GET['userpassword']));
  10. $password = md5("$password2" . ALL_PS);
  11. $email = lib_replace_end_tag(trim($_GET['email']));
  12. if ($username == '' || $password2 == '' || $password == '') {
  13. $res = urlencode("参数有误");
  14. exit(json_encode($res)); //有空信息
  15. }
  16. $sql = "select username from `member` where username='$username'";
  17. $query = mysql_query($sql, $conn);
  18. $count = mysql_num_rows($query);
  19. if ($count > 0) {
  20. exit(json_encode(1)); //返回1表示注册失败
  21. } else {
  22. $addsql = "insert into `member` (username,password,email) values ('$username','$password','$email')";
  23. mysql_query($addsql);
  24. exit(json_encode(0)); //返回0表示注册成功
  25. }
  26. break;
  27. //查询用户信息
  28. case"selectuserinfo";
  29. $username = lib_replace_end_tag($_GET['username']);
  30. $sql = "select id,username,nickname,mobile from `member` where username='$username'";
  31. $query = mysql_query($sql, $conn);
  32. $row = mysql_fetch_array($query);
  33. foreach ($row as $key => $v) {
  34. $res[$key] = urlencode($v);
  35. }
  36. exit(json_encode($res));
  37. break;
  38. //会员登录
  39. case"userlogin";
  40. $username = lib_replace_end_tag($_GET['username']);
  41. $password2 = lib_replace_end_tag(trim($_GET['userpassword']));
  42. $password = md5("$password2" . ALL_PS);
  43. $sqluser = "select id,username,password from `member` where username='" . $username . "' and password='" . $password . "'";
  44. $queryuser = mysql_query($sqluser);
  45. $rowuser = mysql_fetch_array($queryuser);
  46. if ($rowuser && is_array($rowuser) && !empty($rowuser)) {
  47. if ($rowuser['username'] == $username && $rowuser['password'] == $password) {
  48. if ($rowuser['password'] == $password) {
  49. $res = urlencode("登录成功");
  50. exit(json_encode($res));
  51. } else {
  52. $res = urlencode("密码错误");
  53. exit(json_encode($res));
  54. }
  55. } else {
  56. $res = urlencode("用户名不存在");
  57. exit(json_encode($res));
  58. }
  59. } else {
  60. $res = urlencode("用户名密码错误");
  61. exit(json_encode($res));
  62. }
  63. /*
  64. * 0:表示登录成功,1:表示密码错误,2:用户名不存在,3:用户名密码错误
  65. */
  66. break;
  67. default:
  68. exit(json_encode(error));
  69. }
  70. ?>

客户端例子:

  1. <?php
  2. header('Content-Type:text/html;charset=utf-8'); //避免输出乱码
  3. function httpPost($url, $parms) {
  4. $url = $url . $parms;
  5. if (($ch = curl_init($url)) == false) {
  6. throw new Exception(sprintf("curl_init error for url %s.", $url));
  7. }
  8. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  9. curl_setopt($ch, CURLOPT_HEADER, 0);
  10. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 600);
  11. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  12. if (is_array($parms)) {
  13. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data;'));
  14. }
  15. $postResult = @curl_exec($ch);
  16. $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  17. if ($postResult === false || $http_code != 200 || curl_errno($ch)) {
  18. $error = curl_error($ch);
  19. curl_close($ch);
  20. throw new Exception("HTTP POST FAILED:$error");
  21. } else {
  22. // $postResult=str_replace("\xEF\xBB\xBF", '', $postResult);
  23. switch (curl_getinfo($ch, CURLINFO_CONTENT_TYPE)) {
  24. case 'application/json':
  25. $postResult = json_decode($postResult);
  26. break;
  27. }
  28. curl_close($ch);
  29. return $postResult;
  30. }
  31. }
  32. $postUrl = "http://pujia.test.com/api/server.php";
  33. $p=$_GET['p'];
  34. if ($p =="selectuserinfo") {
  35. $username = $_GET['username'];
  36. $parms = "?action=selectuserinfo&username=" . $username . "";
  37. } elseif ($p =="adduserinfo") {
  38. $username = $_GET['username'];
  39. $userpassword = $_GET['userpassword'];
  40. $parms = "?action=adduserinfo&username=" . $username . "&userpassword=" . $userpassword . "";
  41. } elseif ($p =="userlogin") {
  42. $username = $_GET['username'];
  43. $userpassword = $_GET['userpassword'];
  44. $parms = "?action=userlogin&username=" . $username . "&userpassword=" . $userpassword . "";
  45. }
  46. $res = httpPost($postUrl, $parms); //$parms
  47. $res = json_decode($res);
  48. print_r(urldecode(json_encode($res)));
  49. ?>


声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号