API Development Fourth: Defining client/server interface protocols

Source: Internet
Author: User

在进行API开发的时候,需要事先定义好app与server交互的数据格式,这样前端人员与服务端人员才能够事先决定好如何获取数据、如何解析数据、如何传输协议。在我看来目前接口协议无外乎这三种情况:     1. json数据进行交互     2. xml数据进行交互     3. 自定义数据格式交互     自定义数据格式进行前后端的数据交互,需要花费较大的精力,而且需要很有经验的人设计的协议才会确保各个平台的兼容以及良好的可阅读性。并且解析、封装都需要自己来用代码实现,很多第三方库都没办法用上。因为这里我不进行讨论。主要说说json与xml作为交互数据。     我在我的response中封装了json与xml。客户端可以根据自己的需要进行选择是获取json数据还是xml。指定方法是在请求的url中指定format=json/xml。这里为了程序的健壮,我会默认指定一种数据格式的返回,也就是如果客户端没有或者忘记设置format时,默认返回json数据。     **json数据的封装,在php中用json来进行数据交互是非常方便。实现代码:**
    Private Static  function jsonsucencode($code, $msg, $datas){        if(!is_numeric ($code)){return "'; }$ret=Array(' Succode '=$code,' sucmsg '=$msg,' Datas '=$datas,        );EchoJson_encode ($ret); }
$code表示返回的状态码,这个状态码应该在文档中进行说明含义,并事先定义好$msg表示返回的信息,我觉得这个字段是有必要的,至少方便客户端开发人员进行错误定位。备注:我最开始就没有设计这个字段,而只是设计了$code这个状态吗,前端人员每次都要到文档中进行查看这个状态码是什么含义,很影响开发速度。$datas:这个就是返回给前端的业务数据了。然后使用json_encode()对这个数据编码后,就是json数据了。xml数据的封装相对来说比较麻烦,方法有很多,我使用的是用字符串进行拼接:
    Private Static  function xmlsucencode($code, $msg, $datas){        if(!is_numeric ($code)){return "'; }$ret=Array(' Succode '=$code,' sucmsg '=$msg,' Datas '=$datas,        );Echo  Self:: XML ($ret); }Private Static  function xml($datas){Header"Content-type:text/xml");$xml="<?xml version= ' 1.0 ' encoding= ' UTF-8 '?> ';$xml.=' <root> ';$xml.= Self:: Createxml ($datas);$xml.=' </root> ';return $xml; }Private Static  function createxml($datas){        $xml=$attr="";foreach($datas  as $k=$v){if(Is_numeric ($k)) {//If k is a number, use it as a property                $attr="Id= ' {$k} '";$k="Item"; }$xml.="<{$k} {$attr}>";$xml. = Is_array ($v) ? Self:: Createxml ($v) :$v;$xml.="</{$k}>"; }return $xml; }

Now that the XML data and JSON data are encapsulated, you should provide a unified interface for the foreground to invoke, control by parameters, return XML data or JSON data. Instead of throwing out two interfaces like this, let the client person choose for themselves. At the same time thrown to two interfaces there is a problem: if you want to replace, replace the name of the interface and its parameters.
Here I define the Unified interface:

     Public Static  function sendencode($code, $msg, $state, $datas= Array(), $type=self::json){        //If the request parameter has a request type set, the requested type is returned, otherwise the type in the parameter is used, and the default is JSON        $type=isset($_get[' format ']) ?$_get[' format '] :$type;Switch($type){//JSON format data             Case  Self:: JSON:if($state== Self:: SUCCESS) { Self:: Jsonsucencode ($code,$msg,$datas); }Else{ Self:: Jsonerrencode ($code,$msg); }Exit; Break;//XML data             Case  Self:: XML:if($state== Self:: SUCCESS) { Self:: Xmlsucencode ($code,$msg,$datas); }Else{ Self:: Xmlerrencode ($code,$msg); }Exit; Break;//Other data format requests            default: Self:: Jsonerrencode ( Self:: Illegal_code, Self:: illegal_msg);Exit; Break; }    }

There are some constants here, I am defined in the response this class inside, here is not written out, I believe you should be able to guess. If someone needs the complete implementation of this tool, you can leave the mailbox, I sent to you, together to learn progress.
Fifth I'll share an example of this part of my app registration in interface design.

API Development Fourth: Defining client/server interface protocols

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.