PHP API interface to get started detailed

Source: Internet
Author: User
An API is a calling interface that the operating system leaves to an application, which enables the operating system to execute commands (actions) of the application by invoking the operating system's APIs. This article is mainly to share with you PHP API interface Introduction, hope to help everyone.

1. Brief description:

API interface Development, in fact, and the usual development logic, but also slightly different;

Usually the idea of using MVC to develop the site is generally by the controller to call the model, the model returns data, and then by the controller to put the data into the view, show to the user;

API development is: using the controller to call the model, the model returns data, in the controller output JSON format string or XML string;

2. Logic code:

Micro MVC Architecture: Controller controllers, modal models, view views, DB classes (classes that access data)

<?php 2 3 class testcontroller{4 private $db = NULL; 5 6 function __construct () {7 require './db.cla Ss.php '; 8 $this db = new db ();      9}10/**12 * @desc return Data * @author wzh14 * @date 2017-02-1915 * @qq 64694306716 */17 Private Function Jsonreturn ($data) {$return = array (status ' = = 200,/* return state, 200 successful, 50 0 failure */20 ' data ' and $data, ' message ' = ' success ', ' (Json_encode Turn);d ie;24}25/**27 * @desc return Data * @author wzh29 * @date 2017-02-1930 * @qq 6469430673 1 */32 Private Function Success ($message) {$return = array ("status" = + 200,/* return State, 200 successful, 500 failed */35 ' message ' = $message, (json_encode), Notoginseng Echo ($return);d ie;38}39 /**42 * @desc return Data * @author WZH44 * @date 2017-02-1945 * @qq 64694306746 */47 Private Function error ($message) {$return = array (+ ' status ' = 500,/* return Back to status, 200 success, 500 failure */50 ' message ' = $message, Wuyi); Echo Json_encode ($return);d ie;53}5 4/**57 * @desc Test API interface to get all students under this class from class ID * @author wzh59 * @version 1.060 * @date 2017-0 2-1961 */62 Public Function getList () {$class _id = (int) $_get[' class_id '];64 $sql = "Select S  Tudent_id,student_name,gander from student where class_id = ' $class _id ' and is_delete = 0 "; $list = $this GetAll ($sql),}69 (Empty ($list)) {error (' no data ') $this-      List '] = $list, $this jsonreturn ($data),}72/**74 * @desc Test API interface Gets whether the learner has clocked in 75 * @author wzh76 * @version 1.077 * @date 2017-02-1978 * @qq 64694306779 */80 public Function Getsi Gnstatus () {Bayi $student_id = (int) $_get[' student_id '];82 $time = Time (), $start _time = strtotime (Date (' y-m-d ', $time). ' 00:00:00 '); $end _time = $start _time = 3600 * 24;85 $sql = "Select status from Student_status where Stu             dent_id = ' $student _id ' "; $status = $this, db, GetOne ($sql); if ($status = = 1) {88 $this-Success (' clocked in '),}else{90 $this, error (' Not clocked in '); 91}92}93 94}

Summary: The above code shows that the logic of writing code in peace is the same.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.