想要本身一我的完成app,那麼後臺接口也必須本身動動手。不用擔憂,其實很簡單的,給本身信心!
下面就以登陸註冊爲例,作一個api接口 php
首先在mac上搭建PHP環境,下載 MAMP Pro for Mac 3.4 破解版:
http://www.ifunmac.com/2015/08/mamp-pro-3-4/
便可一鍵安裝Apache/PHP/MySQL開發環境。簡單吧。
有了環境就能夠寫代碼了: mysql
首先寫一個Config.php (配置數據庫) git
<?php
//定義數據庫鏈接所需的變量
define("DB_HOST", "localhost");
define("DB_USER", "root");
define("DB_PASSWORD", "master12!");
define("DB_DATABASE", "loginAPI");
?>
github
寫一個DB_Connect.php(用於鏈接數據庫) sql
<?php
class DB_Connect
{
public $con;
8 function __construct()
{
}
function __destruct()
{
// $this->close();
}
//鏈接數據庫
public function connect()
{
require_once 'include/Config.php';
//鏈接mysql
$this->con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE) or die(mysqli_error($this->con));
if (mysqli_connect_errno()) {
die("Database connection failed");
}
// 返回 database handler
return $this->con;
}
//關閉數據鏈接
public function close()
{
mysqli_close($this->con);
}
}
?>
數據庫
再來一個:DB_Functions.php (用來封裝 執行sql後 返回數據的方法) json
<?php
class DB_Functions {
private $db;
// constructor
function __construct() {
require_once 'DB_Connect.php';
// connecting to database
$this->db = new DB_Connect();
$this->db->connect();
}
// destructor
function __destruct() {
}
/**
* 添加用戶信息
*/
public function storeUser($name, $email, $password) {
$uuid = uniqid('', true);
$hash = $this->hashSSHA($password);
$encrypted_password = $hash["encrypted"]; // 加密後的密文
$salt = $hash["salt"]; // salt
$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())");
// 檢查結果
if ($result) {
// 獲取用戶信息
$uid = mysqli_insert_id($this->db->con); // 獲取最新的id
$result = mysqli_query($this->db->con,"SELECT * FROM users WHERE uid = $uid");
//返回剛插入的用戶信息
return mysqli_fetch_array($result);
} else {
return false;
}
}
/**
* 經過email和password獲取用戶信息
*/
public function getUserByEmailAndPassword($email, $password) {
$result = mysqli_query($this->db->con,"SELECT * FROM users WHERE email = '$email'") or die(mysqli_connect_errno());
// check for result
$no_of_rows = mysqli_num_rows($result);
if ($no_of_rows > 0) {
$result = mysqli_fetch_array($result);
$salt = $result['salt'];
$encrypted_password = $result['encrypted_password'];
$hash = $this->checkhashSSHA($salt, $password);
// check for password
if ($encrypted_password == $hash) {
return $result;
}
} else {
return false;
}
}
/**
* 經過email判斷用戶是否存在
*/
public function isUserExisted($email) {
$result = mysqli_query($this->db->con,"SELECT email from users WHERE email = '$email'");
$no_of_rows = mysqli_num_rows($result);
if ($no_of_rows > 0) {
// 用戶存在
return true;
} else {
//用戶不存在
return false;
}
}
/**
* 加密
* @param password
* returns salt and encrypted password
*/
public function hashSSHA($password) {
$salt = sha1(rand());
$salt = substr($salt, 0, 10);
$encrypted = base64_encode(sha1($password . $salt, true) . $salt);
$hash = array("salt" => $salt, "encrypted" => $encrypted);
return $hash;
}
/**
* 解密
* @param salt, password
* returns hash string
*/
public function checkhashSSHA($salt, $password) {
$hash = base64_encode(sha1($password . $salt, true) . $salt);
return $hash;
}
}
?>
api
在Index.php調用並輸出返回值(這個文件地址就是接口的訪問地址) 數組
<?php
if (isset($_POST['tag']) && $_POST['tag'] != '') {
// tag是接口請求時post的值(方法名稱),用來區別調用方法
$tag = $_POST['tag'];
//引用DB_Functions.php
require_once 'include/DB_Functions.php';
$db = new DB_Functions();
// 定義輸入數組
$response = array("tag" => $tag, "error" => FALSE);
// 判斷tag值
if ($tag == 'login') {
//獲取login方法的post參數
$email = $_POST['email'];
$password = $_POST['password'];
// 經過email 和password獲取用戶信息
$user = $db->getUserByEmailAndPassword($email, $password);
if ($user != false) {
//找到用戶信息
$response["error"] = FALSE;
$response["uid"] = $user["unique_id"];
$response["user"]["name"] = $user["name"];
$response["user"]["email"] = $user["email"];
$response["user"]["created_at"] = $user["created_at"];
$response["user"]["updated_at"] = $user["updated_at"];
echo json_encode($response);
} else {
//沒有找到用戶信息
//輸出錯誤信息
$response["error"] = TRUE;
$response["error_msg"] = "賬號或密碼不正確!";
echo json_encode($response);
}
} else if ($tag == 'register') {
//註冊賬號
$name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];
// 判斷用戶是否存在
if ($db->isUserExisted($email)) {
// 若是用戶存在就返錯誤提示
$response["error"] = TRUE;
$response["error_msg"] = "用戶已存在";
echo json_encode($response);
} else {
// 新增用戶
$user = $db->storeUser($name, $email, $password);
if ($user) {
//新增成功返回用戶信息
$response["error"] = FALSE;
$response["uid"] = $user["unique_id"];
$response["user"]["name"] = $user["name"];
$response["user"]["email"] = $user["email"];
$response["user"]["created_at"] = $user["created_at"];
$response["user"]["updated_at"] = $user["updated_at"];
echo json_encode($response);
} else {
// 新增失敗,返回錯誤信息
$response["error"] = TRUE;
$response["error_msg"] = "服務器繁忙,操做失敗";
echo json_encode($response);
}
}
} else {
// tag值無效時
$response["error"] = TRUE;
$response["error_msg"] = "未找到您要的方法";
echo json_encode($response);
}
} else {
$response["error"] = TRUE;
$response["error_msg"] = "您的參數不正確!";
echo json_encode($response);
}
?>
服務器