一步一步学swift之:自己写Api接口-PHP
2015-12-08 20:32
549 查看
想要自己一个人完成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 (配置数据库)
写一个DB_Connect.php(用于连接数据库)
再来一个:DB_Functions.php (用来封装 执行sql后 返回数据的方法)
在Index.php调用并输出返回值(这个文件地址就是接口的访问地址)
下面就以登录注册为例,做一个api接口
首先在mac上搭建PHP环境,下载 MAMP Pro for Mac 3.4 破解版:
http://www.ifunmac.com/2015/08/mamp-pro-3-4/ 即可一键安装Apache/PHP/MySQL开发环境。简单吧。
有了环境就可以写代码了:
首先写一个Config.php (配置数据库)
<?php //定义数据库连接所需的变量 define("DB_HOST", "localhost"); define("DB_USER", "root"); define("DB_PASSWORD", "master12!"); define("DB_DATABASE", "loginAPI"); ?>
写一个DB_Connect.php(用于连接数据库)
<?php class DB_Connect { public $con; 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后 返回数据的方法)
<?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; } } ?>
在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); } ?>
相关文章推荐
- 如何创建swift工程
- swift 创建UICollectionView
- swift -懒加载创建view
- swift实际使用中遇到的问题及解决(2)
- Swift学习笔记 -- 流程控制
- swift UITabelVIew - 纯代码自定义tabelViewCell
- swift编码出现Call can throw, but it is not marked with 'try' and the error is not handled错误的解决
- Swift学习之路 -- 枚举和结构体
- 【菜鸟初学Swift】IOS平台常用传感器的使用方式
- Swift!代码实现UI基础控件
- Swift----单列的正确编写方式
- swift 学习之 UIAlertViewController
- 如何使用Instruments诊断App(Swift版):起步
- swift - 动态计算文本高度
- swift学习之UITabelView ----使用xib定义cell
- swift学习之UITabelView ----UITableViewCell
- swift学习之-- UIAlertVIewController - uiactionsheet
- swift学习之-- UIAlertViewController -alert
- Swift中的元组(turple)
- Swift 开源那点事