php接口返回xml格式
程序员文章站
2022-03-26 12:35:54
...
在头部加上
header("Content-type: application/xml");
例子:
$res = $this->arrayToXml($data,$amount['count'],$page_count,$page);
header("Content-type: text/xml");
echo $res;
//数组转XML
public function arrayToXml($data,$amount,$page_count,$page)
{
// var_dump($data);die;
$xml = "<response><code>0</code><msg>成功</msg><total_results>{$amount}</total_results><total_page>{$page_count}</total_page><current_page>{$page}</current_page><order_list>";
foreach ($data as $key=>$val)
{
$xml.= "<order><modified></modified><receiver_zip>-</receiver_zip><is_tax>false</is_tax><invoice_type></invoice_type><invoice_title></invoice_title><buyer_cod_fee>0.0</buyer_cod_fee><point_fee></point_fee><coupon_pay></coupon_pay><payments></payments>";
foreach($val['order'] as $k=>$v){
if (is_numeric($v)){
$xml.="<".$k.">".$v."</".$k.">";
}else{
$xml.="<".$k.">".$v."</".$k.">";
}
}
$xml.= "<itemlist>";
foreach($val['itemlist'] as $key=>$vv){
$xml.= "<item>";
foreach($vv as $kk=>$vs){
$xml.="<".$kk.">".$vs."</".$kk.">";
}
$xml.= "<shop_sku_id></shop_sku_id><sku_name></sku_name><oid></oid><amount></amount></item>";
}
$xml.= "</itemlist>";
$xml.= "</order>";
}
$xml.="</order_list></response>";
return trim($xml);
}
返回的结果:
上一篇: xml学习笔记
下一篇: C语言指针习题——拆分句子 排序输出