php接口返回xml格式

在头部加上

  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);
    }

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注