PHP Development App Interface method

Source: Internet
Author: User

Using PHP to generate the app interface data is very simple, if you do not know PHP is not related, only need to read the basic PHP syntax, and then look at this example can be.

The app interface is generally JSON (and, of course, a few XML formats) that follow the restful specification:

    {        code:200,        msg: "This is the cue data",        data:[here is the array data]    }

To be able to see the effect first, here is the simplest and simplest demo code
The following is a basic complete demo code, with cache and database query. First look at the simple:
Two files: One encapsulates the method of generating JSON, and the other is the request interface file:

response.php

<?php class response{    /**     * @param $code Status Code     * @param string $message  message     * @param array $data data c10/>* return String     * Output Communication data as JSON *    /public static function json ($code, $message = ", $data =array ()) {        if (!is_numeric ($code)) {            return ';        }        $result = Array (          ' code ' = = $code,          ' message ' = $message,            ' data ' = $data        );        echo Json_encode ($result);        Exit;    }}

test.php

<?phprequire_once ('./response.php '); $arr = Array (    ' id ' = = 1,    ' name ' = ' Singwa '); Response::json (200, "Data return Success", $arr);

Two files, completed a simple Request interface demo, now test, I installed a local phpstudy, put these two files in the WWW root directory, browser input interface address

http://localhost/test.php

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.