PHP封裝xml資料通訊介面

來源:互聯網
上載者:User

標籤:style   http   io   ar   使用   sp   for   on   資料   

PHP 產生XML資料
1)組裝字串 :簡單,容易理解,常用
2)使用系統類別
如:
 DomDocument
 
下面詳細說明 組裝字串 產生XML資料
xml資料需要1.頭資訊<?xml
           2.根節點
           3.資料
<?php 
 class Response{
/**
*按json方式輸出通訊資料
*@param integer $code 狀態代碼
*@param string $message 提示資訊
*@param array $data 資料
*return string 返回值為json
*/
//靜態方法,構造json資料
 
   public static function xml(){
  $xml="<?xml version=‘1.0‘ encoding=‘UTF-8‘?>";
  $xml.="<root>";//用點好和前面的字串連結,這個是根節點
  $xml.="<code>200</code>";
  $xml.="<message>資料返回成功</message>";
  $xml.="<data>";
  $xml.="<id>1</id>";
  $xml.="<name>david</name>";
  $xml.="</data>";
  $xml.="</root>";


  echo $xml;


   }
}
Response::xml();
?>


運行網頁顯示:
200資料返回成功1david
查看原始碼:
<?xml version=‘1.0‘ encoding=‘UTF-8‘?><root><code>200</code><message>


資料返回成功


</message><data><id>1</id><name>david</name></data></root>


瀏覽器中查看源碼時資料緊靠在一起,不直觀。所以每個資料輸出後換行。


 $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>david</name>\n";
  $xml.="</data>\n";
  $xml.="</root>\n";


修改後:
<?xml version=‘1.0‘ encoding=‘UTF-8‘?>
<root>
<code>200</code>
<message>資料返回成功</message>
<data>
<id>1</id>
<name>david</name>
</data>
</root>




預設情況下:xml標籤不會在瀏覽器頁面中顯示,只會在查看原始碼時顯示
因為預設情況下http回應標頭資訊是html類型。預設: header("Content-


Type:text/html");
1.png


如果需要在頁面上顯示xml標籤,就需要修改回應標頭資訊中content type為 


text/xml
public static function 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>david</name>\n";
  $xml.="</data>\n";
  $xml.="</root>\n";


  echo $xml;


   }






PHP xml方式封裝介面資料方法
封裝方法:
xmlEncode($code,$message="",$data=array())




複雜數組解析不出來:


<?php 
 class Response{
 
   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>\n";
   echo $xml;
   }
 public static function xmlToEncode($data){
//解析數組,拼裝成xml資料
$xml= "";
foreach($data as $key=>$value){
$xml.= "<{$key}>";//大括弧裡面可以被識別為變數
   $xml.= $value;
$xml.="</{$key}>";
}
return $xml;
}


}
$data=array(
‘id‘=> 1,
‘name‘=>‘david‘
);
Response::xmlEncode(200,‘success‘,$data);
?>


出現:
<root>
<code>200</code>
<message>success</message>
<data>Array</data>
</root>
 
當foreach迴圈到最後一個資料時$value為數組,所以不會顯示出來,因此需要


遞迴的調用xmlToEncode($value)
就是講上面的代碼 
 $xml.= is_$value;改為:
 $xml.=is_array($value)?self::xmlToEncode($value):$value;
結果:
<root>
<code>200</code>
<message>success</message>
<data>
<id>1</id>
<name>david</name>
</data>
</root>


如果要封裝的資料不是key=>value形式:
(1,2,3)會自動封裝成xml <0>1</0><1>2</1><2>2</2> 數字不能作標籤所以


報錯
可以轉換成:
<item id="0">1</item>形式


最終版:
<?php 
 class Response{
/**
*按json方式輸出通訊資料
*@param integer $code 狀態代碼
*@param string $message 提示資訊
*@param array $data 資料
*return string 返回值為json
*/
//靜態方法,構造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;
   }*/
   /*public static function 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>david</name>\n";
  $xml.="</data>\n";
  $xml.="</root>\n";


  echo $xml;


   }*/
   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>\n";
   echo $xml;
   }
 public static function xmlToEncode($data){
//解析數組,拼裝成xml資料
$xml= $attr="";

foreach($data as $key=>$value){
if(is_numeric($key)){
   $attr=" id=‘{$key}‘";
   $key="item";
}
$xml.= "<{$key}{$attr}>";//大括弧裡面可以被識別為變數
   $xml.=is_array($value)?self::xmlToEncode($value):$value;
$xml.="</{$key}>";
}
return $xml;
}


}
$data=array(
‘id‘=> 1,
‘name‘=>‘david‘,
‘type‘=>array(4,5,6)
);
Response::xmlEncode(200,‘success‘,$data);
?>

PHP封裝xml資料通訊介面

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.