php開發APP介面方法

來源:互聯網
上載者:User

使用PHP來產生APP介面資料是非常簡單的,如果你還不瞭解PHP沒有關係,只需要看過PHP的基本文法,再看本樣本就可以了。

APP介面一般都是json格式(當然也有少數xml格式)遵循restful規範的:

    {        code:200,        msg:"這個是提示資料",        data:[這裡是數組資料]    }

為了能立竿見影先看到效果,這裡貼一個最簡單最簡單的一個demo代碼
後面再貼一個基本完整的demo代碼,帶緩衝和資料庫查詢的。先看簡單的:
兩個檔案:一個封裝了產生json的方法,一個是請求介面檔案:

Response.php

<?php class Response{    /**     * @param $code 狀態代碼     * @param string $message  提示資訊     * @param array $data 資料     * return string     * 按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,"資料返回成功",$arr);

兩個檔案,就完成了一個最簡單的請求介面demo,現在測試一下,我本地安裝了個PHPstudy,將這兩個檔案放在www根目錄裡,瀏覽器輸入介面地址

http://localhost/test.php

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.