<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                [TOC] ##1)JSON方式封裝通信接口 ###**a)PHP生成JSON數據** ~~~php $data = "輸出json數據"; // 字符串的編碼轉換 iconv(in_charset, out_charset, str) // $newData = iconv('UTF-8', 'GBK', $data); // 這個函數只能接受utf-8的編碼的數據,如果傳遞其他格式的數據該函數會返回null echo json_encode($data); ~~~ ###**b)通信數據標準格式** ~~~ 返回格式需要包括: // code 狀態碼(200,400等) // message 提示信息(郵箱格式不正確;數據返回成功等) // data 返回數據 ~~~ *封裝接口方法路徑:app/response.PHP* ~~~ class Response { /** * [json 按json方式輸出通信數據] * @Author ZJC * @DateTime 2017-02-14T11:58:07+0800 * @param [integer] $code [狀態碼] * @param string $message [提示信息] * @param array $data [數據] * @return [sting] [返回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; } } ~~~ *封裝路徑:app/test.php* ~~~ require_once('./response.php'); $arr = array( 'id' => 1, 'name' => 'singwa' ); Response::json(200, '數據返回成功', $arr); ~~~ ##2)PHP生成XML數據 ###**a)PHP生成XML數據** 組裝字符串 使用系統類(DomDocument、XMLWriter、SimpleXML) ###**b)XML方式封裝接口數據方法** ~~~ class Response { public static function xml() { // 記住要加header,發送原生的 HTTP 頭,才能在網頁上查看到標準的XML格式 header('Content-Type:text/xml'); $xml = "<?xml version='1.0' encoding='UTF-8'?>\n"; $xml .= "<root>\n"; $xml .= "<code>200</code>\n"; $xml .= "<message>數據返回成功</message>\n"; $xml .= "<data>\n"; $xml .= "<id>1</id>\n"; $xml .= "<name>singwa</name>\n"; $xml .= "</data>\n"; $xml .= "</root>"; echo $xml; } } Response::xml(); ~~~ ##3)XML方式封裝通信接口 ###**XML方式封裝接口數據方法** ~~~ class Response { /** * [xmlEncode 按xml方式輸出通信數據] * @Author ZJC * @DateTime 2017-02-14T11:58:07+0800 * @param [integer] $code [狀態碼] * @param string $message [提示信息] * @param array $data [數據] * @return [sting] [返回json數據] */ public static function xmlEncode($code, $message, $data = array()) { if (!is_numeric($code)) { return ''; } $result = array( 'code' => $code, 'message' => $message, 'data' => $data ); header("Content-Type:text/xml"); $xml = "<?xml version='1.0' encoding='UTF-8'?>\n"; $xml .= "<root>\n"; $xml .= self::xmlToEncode($result); $xml .= "</root>"; echo $xml; } // 把數組轉換成XML節點 public static function xmlToEncode($data) { $xml = $attr = ""; foreach ($data as $key => $value) { // 健值為數字,轉換成<item id="{$key}"></item> if(is_numeric($key)) { $attr = " id='{$key}'"; $key = "item"; } $xml .= "<{$key}{$attr}>"; $xml .= is_array($value) ? self::xmlToEncode($value) : $value; $xml .="</{$key}>\n"; } return $xml; } } //實例 $data = array( 'id' => 1, 'name' => 'singwa', 'type' => array(4,5,6), //數組里沒寫健值,因此健值默認為0,1,2的情況,需要轉換成<item id="0"></item> 'test' => array(1,45,67=>array(123, 'test')), ); Response::xmlEncode(200, 'success', $data); ~~~ ##4)綜合方式封裝通信數據方法 封裝方法:show($code, $message, $data=array(), $type='json') ~~~ class Response { // 常量為默認返回數據類型 const JSON = 'json'; /** * [show 綜合通信方法] * @Author ZJC * @DateTime 2017-02-14T16:57:48+0800 * @param [type] $code [狀態碼] * @param string $message [提示信息] * @param array $data [數據] * @param [type] $type [數據類型] * @return [type] [返回json數據] */ public static function show($code, $message = '', $data = array(), $type = self::JSON) { if(!is_numeric($code)) { return ''; } $type = isset($_GET['format']) ? $_GET['format'] : self::JSON; $result = array( 'code' => $code, 'message' => $message, 'data' => $data ); if($type == 'json') { self::json($code, $message, $data); exit; } elseif($type == 'array') { // 調試查看傳遞過來的數據 var_dump($result); } elseif($type == 'xml') { self::xmlEncode($code, $message, $data); exit; } else { // TODO } } // 下面代碼為json和xml的封裝方法,此處省略 } ~~~ *調用路徑:app/test.php* ~~~ require_once('./response.php'); $data = array( 'id' => 1, 'name' => 'singwa', 'type' => array(4,5,6), 'test' => array(1,45,67=>array(123, 'test')), ); Response::show(200, 'success', $data, 'array'); ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看