想要自己一個人完成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 ?>