Home  >  Article  >  Backend Development  >  Code examples for php development API interfaces (registration, login, query, etc.)

Code examples for php development API interfaces (registration, login, query, etc.)

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

2、客户端例子:

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

以上就是今天php 教程给出的示例代码,用php开发简单的API接口,希望对大家有所帮助。 程序员之家,专为心你每一天。



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn