php之API介面入門詳解

小云云
發布: 2023-03-22 11:24:02
原創
13129 人瀏覽過

API就是作業系統留給應用程式的一個呼叫接口,應用程式透過呼叫作業系統的 API 而使作業系統去執行應用程式的指令(動作)。本文主要和大家分享php之API介面入門詳解,希望能幫助大家。

1.簡述:

api介面開發,其實和平時開發邏輯差不多;但是也有略微差異;

平時使用mvc開發網站的思路一般是都由控制器去調用模型,模型返回數據,再由控制器把數據放到視圖中,展現給用戶;

api開發是:使用控制器去調用模型,模型返回數據,在有控制器輸出json格式字串或XML 字串;

2.邏輯程式碼:

微型mvc結構:controller 控制器,modal 模型,view 視圖,db類別(存取資料的類別)

<?php
 2 
 3 class TestController{
 4     private $db = null;
 5 
 6     function __construct(){
 7         require &#39;./db.class.php&#39;;
 8         $this -> db = new db();
 9     }
10 
11     /**
12      * @desc 返回数据
13      * @author wzh
14      * @date 2017-02-19
15      * @qq 646943067
16      */
17     private function jsonReturn($data){
18         $return = array(
19             &#39;status&#39; => 200,    /* 返回状态,200 成功,500失败 */
20             &#39;data&#39; => $data,
21             &#39;message&#39; => &#39;获取成功&#39;,
22         );
23         echo json_encode($return);die;
24     }
25 
26     /**
27      * @desc 返回数据
28      * @author wzh
29      * @date 2017-02-19
30      * @qq 646943067
31      */
32     private function success($message){
33         $return = array(
34             &#39;status&#39; => 200,    /* 返回状态,200 成功,500失败 */
35             &#39;message&#39; => $message,
36         );
37         echo json_encode($return);die;
38     }
39 
40 
41     /**
42      * @desc 返回数据
43      * @author wzh
44      * @date 2017-02-19
45      * @qq 646943067
46      */
47     private function error($message){
48         $return = array(
49             &#39;status&#39; => 500,    /* 返回状态,200 成功,500失败 */
50             &#39;message&#39; => $message,
51         );
52         echo json_encode($return);die;
53     }
54 
55 
56     /**
57      * @desc 测试api接口 根据 班级id获取该班级下的所有学员
58      * @author wzh
59      * @version 1.0
60      * @date 2017-02-19
61      */
62     public function getList(){
63         $class_id = (int) $_GET[&#39;class_id&#39;];
64         $sql = " select student_id,student_name,gander from student where class_id = &#39;$class_id&#39; and is_delete = 0 ";
65         $list = $this -> db -> getAll($sql);
66         if(empty($list)){
67             $this -> error(&#39;暂无数据&#39;);
68         }
69         $data[&#39;list&#39;] = $list;
70         $this -> jsonReturn($data);
71     }
72 
73     /**
74      * @desc 测试api接口 获取该学员 是否 已经打卡
75      * @author wzh
76      * @version 1.0
77      * @date 2017-02-19
78      * @qq 646943067
79      */
80     public function getSignStatus(){
81         $student_id = (int) $_GET[&#39;student_id&#39;];
82         $time = time();
83         $start_time = strtotime(date(&#39;Y-m-d&#39;,$time) . &#39; 00:00:00&#39;);
84         $end_time = $start_time = 3600 * 24;
85         $sql = " select status from student_status where student_id = &#39;$student_id&#39; ";
86         $status = $this -> db -> getOne($sql);
87         if($status == 1){
88             $this -> success(&#39;已打卡&#39;);
89         }else{
90             $this -> error(&#39;未打卡&#39;);
91         }
92     }
93 
94 }
登入後複製

總結:透過上述程式碼可以看出和平時寫程式碼的邏輯是相同的。

相關推薦:

如何在本機上進行API介面測試

api介面安全驗證功能的實作

#PHP進行API介面測試

以上是php之API介面入門詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!