使用php自己实现所需接口(需要php基础)
2016-07-27 11:27
525 查看
<span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: rgb(255, 255, 255);">php的简单介绍:</span>
php是一脚本语言,功能十分强大,因为其开发速度快,周期短现在被广泛的使用,我们现在很多的服务器都是用php再写。
做android开发的对于我们经常跟服务器的交互肯定不陌生,通常都是通过服务器给定的url通过post,get等方法来请求服务器,然后处理服务器返回的数据,现在广泛使用的是json数据,xml也有,(比如天气预报的接口);
做手机端的可能对于接口这方面没有特别的了解过,经常有需要都是要服务端来做,但是我觉得熟悉他的流程已经编写还是很有必要的。
首先准备工作:
(1)安装一个集成好数据库,apache,以及php的工具wampserver(具体安装过程就不细说了,网上有很多,基本上傻瓜式的),当然也可以分别进行安装,只是配置有点麻烦,可以自己选择;(2)确定工具安装成功,并且可以执行php
(3)php基础(本文是介绍接口,在测试post请求的时候可能需要通过表单提交来进行测试)
下边就一个登陆,注册接口为例来进行下介绍:
(1)编写一个配置文件Config.php;定义数据库连接的写参数,参数分别是主机名,账号,密码,还有数据库名称
<?php define("DB_HOST","localhost"); define("DB_USER","root"); define("DB_PASSWORD","123456"); define("DB_DATABASE","test"); ?>(2)编写数据库连接的管理文件(需要注意的是,以前的mysql开头的方法都已过时,统一使用mysqli开头的方法);
<?php class DB_Connect { public $con; function __construct() { } function __destruct() { // $this->close(); } //连接数据库 public function connect() { require_once '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); } } ?>
(3)编写一个工具类用来处理跟数据库的交互等的操作,文件名DB_Functions.php
<?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 echo $uuid."==".$encrypted_password."==".$salt; echo "<pre>"; print_r($hash); $sql = "INSERT INTO users(unique_id, name, email, encrypted_password, salt, created_at) VALUES('$uuid', '$name', '$email', '$encrypted_password', '$salt', NOW())"; $result = $this->db->con->query($sql); echo $result; // 检查结果 if ($result) { echo "sucess"; // 获取用户信息 $uid = mysqli_insert_id($this->db->con); // 获取最新的id $result = mysqli_query($this->db->con,"SELECT * FROM users WHERE uid = $uid"); //返回刚插入的用户信息 echo "<br>".$uid; $sql = "select * from users"; $result = $this->db->con->query($sql); echo "<pre>"; print_r($result); return true; } else { echo "false"; 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()); echo "<pre>"; print_r($result); // 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; } } ?>(4)编写接口处理类index.php,处理post或get请求,但是要注意,get请求是明文请求,参数都是拼接到接口上的,任何人都能看的到(本文使用post方式)如果要使用get方式,把下边代码中所有_POST改为_GET即可。
<?php if (isset($_POST['tag']) && $_POST['tag'] != '') { $tag = $_POST['tag']; require_once 'DB_Functions.php'; $db = new DB_Functions(); $response = array("tag" => $tag, "error" => FALSE); if ($tag == 'login') { $email = $_POST['email']; $password = $_POST['password']; $user = $db->getUserByEmailAndPassword($email, $password); echo "<br>".$user; if ($user != false) { echo $email."<br>" ; echo $password."<br>"; $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"]; echo json_encode($response); } else{ echo "hahaha"; $response["error"] = TRUE; //如果转json数据的时候没有输出日志,那么说明是编码错误了。如果是汉字必须是utf-8编码 $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 { $response["error"] = TRUE; $response["error_msg"] = "未找到您要的方法"; echo json_encode($response); } } else { $response["error"] = TRUE; $response["error_msg"] = "您的参数不正确!"; echo json_encode($response); } ?>
(5)下边就是来调用接口了,我们可以通过android端来进行测试
看图