本篇文章介绍的是关于PHP服务端开发APP接口 ,现在给大家分享一下,有兴趣的朋友可以看一下


一、APP接口简介


什么是app接口?app接口就是用服务端程序如php写好的脚本,以供app客户端请求而获得数据的一个东西。比如一个商铺app的首页,肯定有一些商品列表,那么当你打开这个app时,这个封装在app里的这个首页其实会去请求一个远程php文件如: 去获得需要展示在首页的商品列表数据。前端工程师拿到这些数据,就会按照特定的设计,将这些内容展示出来了。
接口要实现的目的就是这样。一个app内部通常需要访问多个php接口来获得不同的数据。下面具体讲一讲接口实现的流程以及实现接口需要的一些核心的技术。

二、PHP接口知识JSON和XML方式封装通信接口

response.class.php

<?php/** *description 用于返回指定数据格式的类 *@param $code [int] 返回的状态码 *@param $message [string] 返回的状态信息 *@param $data [array] 需要返回的数据 * */class Response{ public function json($code,$message,$data){ $result = array( "code" => $code, "message" => $message, "data" => $data ); return json_encode($result); } public function xml($code,$message,$data){ $result = array( "code" => $code, "message" => $message, "data" => $data ); header('Content-Type:text/xml'); $xml = "<?xml version='1.0' encoding='UTF-8'?>\n"; $xml .= "<root>"; $xml .= self::encodeXml($result); $xml .= "</root>"; return $xml; } /** *将数据解析为XML字符串 */ public static function encodeXml($data){ $attr = $xml = ""; foreach($data as $key => $value){ if(is_numeric($key)){ $attr = " id='{$key}'"; $key = "item"; } $xml .= "<{$key}{$attr}>"; $xml .= is_array($value)?self::encodeXml($value):$value; $xml .= "</$key>"; } return $xml; } }

response.class.php是一个最简单的返回json或XML格式数据的类
下面贴出接口文件代码:
returndata.php

<?phprequire "response.class.php"; //引入返回信息类//准备返回数据$code = 200;$message = "信息请求成功";$data = array( "name" => "ruanwnewu", "sex" => "1", "age" => "28", "exp" => array( "2012" => "北京瑞泰新", "2013" => "兄弟连", "2014" => "木蚂蚁科技" ) );//实例化response类$response = new Response;//返回数据echo $response -> json($code,$message,$data);三、实际开发例子

开发三个接口(登录、注册、文件上传),分别完成对应的功能

因为本人不做APP开发,所以在实际的接口测试过程中,运用Firefox浏览器的RESTClient扩展模拟APP请求服务和接收数据
(1)登录、注册接口的编写
直接上代码:

<?phprequire ("../connect_db.php");$action = $_REQUEST["action"];$conn = db_connect(); mysql_query("set names 'utf8'"); mysql_select_db("FECG");switch ($action){ case 'login': login(); break; case 'register': register(); break; case 'upload': upload(); break; default: break; }//登录接口function login(){ $account_name = $_POST["username"]; $password = $_POST["password"]; $result = mysql_query("SELECT * FROM app_account WHERE account_name='".$account_name."'"); if (mysql_num_rows($result) > 0){ $row = mysql_fetch_array($result); $salt = $row["salt"]; $new_password = md5($password."".$salt); if ($new_password == $row["password"]){ //登录成功 $current_time = new DateTime(); $login_time = $current_time -> format('Y-m-d H:i:s'); $result = mysql_query("UPDATE app_account SET last_lgin_time='".$login_time."' WHERE account_name='".$row['account_name']."'"); $array = array(); $array["account_id"] = $row["account_id"]; $array["account_name"] = $row["account_name"]; $array["create_time"] = $row["creat_time"]; $json = json_encode(array( "resultCode"=>200, "message"=>"login successed!", "data"=>$array)); echo($json); }else{ $json = json_encode(array( "resultCode"=>500, "message"=>"The password is wrong!please try again." )); echo($json); } }else{ //登录失败 $json = json_encode(array( "resultCode"=>500, "message"=>"please register!" )); echo($json); } }//注册接口function register(){ $account_name = $_POST["username"]; $password = $_POST["password"]; $result = mysql_query("select * from app_account where account_name='".$account_name."'"); //查询失败 if (!$result){ $json = json_encode(array( "resultCode"=>500, "message"=>"select failed!" )); echo($json); } //用户名已经注册 if (mysql_num_rows($result) > 0){ $json = json_encode(array( "resultCode"=>500, "message"=>"register failed!" )); echo($json); }else{ //插入记录到数据库中 $account_id = uniqid(); $salt = uniqid(); $new_password = md5($password."".$salt); $current_time = new DateTime(); $create_time = $current_time -> format('Y-m-d H:i:s'); $last_login_time = $create_time; $result = mysql_query("insert into app_account(account_id,account_name,password,salt,creat_time,last_lgin_time) values('".$account_id."', '".$account_name."', '".$new_password."', '".$salt."', '".$create_time."', '".$last_login_time."')"); $user_id = uniqid(); $result1 = mysql_query("INSERT INTO app_user(user_id,username,account_id) VALUES('".$user_id."', '".$account_name."', '".$account_id."')"); if ($result){ $json = json_encode(array( "resultCode"=>200, "message"=>"register successed!" )); echo($json); } } }//文件上传接口function upload(){}?>