一步一步学swift之:自己写Api接口-PHP

想要自己一个人完成app,那么后台接口也必须自己动动手。不用担心,其实很简单的,给自己信心!
下面就以登录注册为例,做一个api接口

首先在mac上搭建PHP环境,下载 MAMP Pro for Mac 3.4 破解版:

http://www.ifunmac.com/2015/08/mamp-pro-3-4/
即可一键安装Apache/PHP/MySQL开发环境。简单吧。

有了环境就可以写代码了:

首先写一个Config.php (配置数据库)

1 <?php
2  
3  //定义数据库连接所需的变量
4  define("DB_HOST", "localhost");
5  define("DB_USER", "root");
6  define("DB_PASSWORD", "master12!");
7  define("DB_DATABASE", "loginAPI");
8  
9 ?>

写一个DB_Connect.php(用于连接数据库)

 1 <?php
 2  
 3 class DB_Connect
 4 {
 5     public $con;
 6
 8     function __construct()
 9     {
10  
11     }
12 
14     function __destruct()
15     {
16         // $this->close();
17     }
18  
19     //连接数据库
20     public function connect()
21     {
22         require_once 'include/Config.php';
23         //连接mysql
24         $this->con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE) or die(mysqli_error($this->con));
25         if (mysqli_connect_errno()) {
26             die("Database connection failed");
27         }
28  
29         // 返回 database handler
30         return $this->con;
31     }
32  
33     //关闭数据连接
34     public function close()
35     {
36         mysqli_close($this->con);
37     }
38  
39 }
40  
41 ?>

再来一个:DB_Functions.php (用来封装 执行sql后 返回数据的方法)

  1 <?php
  2  
  3 class DB_Functions {
  4  
  5     private $db;
  6  
  7     // constructor
  8     function __construct() {
  9         require_once 'DB_Connect.php';
 10         // connecting to database
 11         $this->db = new DB_Connect();
 12         $this->db->connect();
 13     }
 14  
 15     // destructor
 16     function __destruct() {
 17         
 18     }
 19  
 20     /**
 21      * 添加用户信息
 22      */
 23     public function storeUser($name, $email, $password) {
 24         $uuid = uniqid('', true);
 25         $hash = $this->hashSSHA($password);
 26         $encrypted_password = $hash["encrypted"]; // 加密后的密文
 27         $salt = $hash["salt"]; // salt
 28         $result = mysqli_query($this->db->con,"INSERT INTO users(unique_id, name, email, encrypted_password, salt, created_at) VALUES('$uuid', '$name', '$email', '$encrypted_password', '$salt', NOW())");
 29         // 检查结果
 30         if ($result) {
 31             // 获取用户信息
 32             $uid = mysqli_insert_id($this->db->con); // 获取最新的id
 33             $result = mysqli_query($this->db->con,"SELECT * FROM users WHERE uid = $uid");
 34             //返回刚插入的用户信息
 35             return mysqli_fetch_array($result);
 36         } else {
 37             return false;
 38         }
 39     }
 40  
 41     /**
 42      * 通过email和password获取用户信息
 43      */
 44     public function getUserByEmailAndPassword($email, $password) {
 45         $result = mysqli_query($this->db->con,"SELECT * FROM users WHERE email = '$email'") or die(mysqli_connect_errno());
 46         // check for result 
 47         $no_of_rows = mysqli_num_rows($result);
 48         if ($no_of_rows > 0) {
 49             $result = mysqli_fetch_array($result);
 50             $salt = $result['salt'];
 51             $encrypted_password = $result['encrypted_password'];
 52             $hash = $this->checkhashSSHA($salt, $password);
 53             // check for password
 54             if ($encrypted_password == $hash) {
 55                 return $result;
 56             }
 57         } else {
 58             return false;
 59         }
 60     }
 61  
 62     /**
 63      * 通过email判断用户是否存在
 64      */
 65     public function isUserExisted($email) {
 66         $result = mysqli_query($this->db->con,"SELECT email from users WHERE email = '$email'");
 67         $no_of_rows = mysqli_num_rows($result);
 68         if ($no_of_rows > 0) {
 69             // 用户存在
 70             return true;
 71         } else {
 72             //用户不存在
 73             return false;
 74         }
 75     }
 76  
 77     /**
 78      * 加密
 79      * @param password
 80      * returns salt and encrypted password
 81      */
 82     public function hashSSHA($password) {
 83  
 84         $salt = sha1(rand());
 85         $salt = substr($salt, 0, 10);
 86         $encrypted = base64_encode(sha1($password . $salt, true) . $salt);
 87         $hash = array("salt" => $salt, "encrypted" => $encrypted);
 88         return $hash;
 89     }
 90  
 91     /**
 92      * 解密
 93      * @param salt, password
 94      * returns hash string
 95      */
 96     public function checkhashSSHA($salt, $password) {
 97  
 98         $hash = base64_encode(sha1($password . $salt, true) . $salt);
 99  
100         return $hash;
101     }
102  
103 }
104  
105 ?>

在Index.php调用并输出返回值(这个文件地址就是接口的访问地址)

 1 <?php
 2  
 3 if (isset($_POST['tag']) && $_POST['tag'] != '') {
 4     // tag是接口请求时post的值(方法名称),用来区别调用方法
 5     $tag = $_POST['tag'];
 6  
 7     //引用DB_Functions.php
 8     require_once 'include/DB_Functions.php';
 9     $db = new DB_Functions();
10  
11     // 定义输入数组
12     $response = array("tag" => $tag, "error" => FALSE);
13  
14     // 判断tag值
15     if ($tag == 'login') {
16         //获取login方法的post参数
17         $email = $_POST['email'];
18         $password = $_POST['password'];
19  
20         // 通过email 和password获取用户信息
21         $user = $db->getUserByEmailAndPassword($email, $password);
22         if ($user != false) {
23             //找到用户信息
24             $response["error"] = FALSE;
25             $response["uid"] = $user["unique_id"];
26             $response["user"]["name"] = $user["name"];
27             $response["user"]["email"] = $user["email"];
28             $response["user"]["created_at"] = $user["created_at"];
29             $response["user"]["updated_at"] = $user["updated_at"];
30             echo json_encode($response);
31         } else {
32             //没有找到用户信息
33             //输出错误信息
34             $response["error"] = TRUE;
35             $response["error_msg"] = "帐号或密码不正确!";
36             echo json_encode($response);
37         }
38     } else if ($tag == 'register') {
39         //注册帐号
40         $name = $_POST['name'];
41         $email = $_POST['email'];
42         $password = $_POST['password'];
43  
44         // 判断用户是否存在
45         if ($db->isUserExisted($email)) {
46             // 如果用户存在就返错误提示
47             $response["error"] = TRUE;
48             $response["error_msg"] = "用户已存在";
49             echo json_encode($response);
50         } else {
51             // 新增用户
52             $user = $db->storeUser($name, $email, $password);
53             if ($user) {
54                 //新增成功返回用户信息
55                 $response["error"] = FALSE;
56                 $response["uid"] = $user["unique_id"];
57                 $response["user"]["name"] = $user["name"];
58                 $response["user"]["email"] = $user["email"];
59                 $response["user"]["created_at"] = $user["created_at"];
60                 $response["user"]["updated_at"] = $user["updated_at"];
61                 echo json_encode($response);
62             } else {
63                 // 新增失败,返回错误信息
64                 $response["error"] = TRUE;
65                 $response["error_msg"] = "服务器繁忙,操作失败";
66                 echo json_encode($response);
67             }
68         }
69     } else {
70         // tag值无效时
71         $response["error"] = TRUE;
72         $response["error_msg"] = "未找到您要的方法";
73         echo json_encode($response);
74     }
75 } else {
76     $response["error"] = TRUE;
77     $response["error_msg"] = "您的参数不正确!";
78     echo json_encode($response);
79 }
80 ?>
原文地址:https://www.cnblogs.com/wuxian/p/5030894.html