国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > XML > 正文

laravel輸出xml數(shù)據(jù),php輸出xml格式數(shù)據(jù)

2020-03-22 16:18:44
字體:
供稿:網(wǎng)友
背景:

seo的同事要批量提交xml格式的數(shù)據(jù)到搜索引擎,目前項(xiàng)目用laravel框架開發(fā)的,所以就有了這篇文章的誕生了。網(wǎng)上有不少關(guān)于php輸出xml格式的例子,小弟不才也搬過,只是在php文件上面測試是沒問題的,把它搬到laravel框架里面,就有有坑了,主要原因是header頭的問題。

laravel框架怎么返回xml格式數(shù)據(jù)?

如果用header(“Content-type: text/xml”);

這樣的話是沒有效果的,會提示這樣的錯(cuò)誤:

This page contains the following errors:

error on line 14 at column 6: XML declaration allowed only at the start of the document

Below is a rendering of the page up to the first error.

laravel框架在輸出xml的時(shí)候會自行用text/html方式返回?cái)?shù)據(jù),解決辦法:

需要return response($xml,200)->header(“Content-type”,“text/xml”);這樣的方式才能改變header頭

laravel返回xml數(shù)據(jù)格式例子:

/**  * 神馬搜索數(shù)據(jù)結(jié)構(gòu)化,written:yangxingyi Data:2018-10-25 11:15  */ public function index(Request $request){        $data_array = array(            array(                'title' => 'title1',                'content' => 'content1',                'pubdate' => '2009-10-11',            ),            array(                'title' => 'title2',                'content' => 'content2',                'pubdate' => '2009-11-11',            )        );        $title_size = 1;        $xml = "<?xml version=/"1.0/" encoding=/"utf-8/"?>/n";        $xml .= "<article>/n";        foreach ($data_array as $data) {            $xml .= $this->create_item($data['title'], $title_size, $data['content'], $data['pubdate']);        }        $xml .= "</article>/n";        #echo $xml;        return response($xml,200)->header("Content-type","text/xml");    } /**  * 神馬搜索數(shù)據(jù)結(jié)構(gòu)化,節(jié)點(diǎn)的具體內(nèi)容 written:yangxingyi  */    private function create_item($title_data, $title_size, $content_data, $pubdate_data)    {        $item = "<item>/n";        $item .= "<title size=/"" . $title_size . "/">" . $title_data . "</title>/n";        $item .= "<content>" . $content_data . "</content>/n";        $item .= " <pubdate>" . $pubdate_data . "</pubdate>/n";        $item .= "</item>/n";        return $item;    }

PHP生成xml格式的數(shù)據(jù)直接加上 header(“Content-type: text/xml”);頭就行了

<?php header("Content-type: text/xml");$data_array = array(    array(    'title' => 'title1',    'content' => 'content1',        'pubdate' => '2009-10-11',    ),    array(    'title' => 'title2',    'content' => 'content2',    'pubdate' => '2009-11-11',    ));$title_size = 1;$xml = "<?xml version=/"1.0/" encoding=/"utf-8/"?>/n";$xml .= "<article>/n";foreach ($data_array as $data) {$xml .= create_item($data['title'], $title_size, $data['content'], $data['pubdate']);}$xml .= "</article>/n";echo $xml;//創(chuàng)建XML單項(xiàng)function create_item($title_data, $title_size, $content_data, $pubdate_data){    $item = "<item>/n";    $item .= "<title size=/"" . $title_size . "/">" . $title_data . "</title>/n";    $item .= "<content>" . $content_data . "</content>/n";    $item .= " <pubdate>" . $pubdate_data . "</pubdate>/n";    $item .= "</item>/n";    return $item;}?>

更多PHP相關(guān)知識,請?jiān)L問PHP教程!

以上就是laravel輸出xml數(shù)據(jù),php輸出xml格式數(shù)據(jù)的詳細(xì)內(nèi)容,更多請關(guān)注電腦知識網(wǎng)其它相關(guān)文章!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時(shí)間聯(lián)系我們修改或刪除,多謝。

上一篇:返回列表

下一篇:XPath快速解析XML

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 曲水县| 潞西市| 连江县| 祁连县| 邯郸县| 哈密市| 云和县| 右玉县| 长葛市| 香河县| 桃源县| 太和县| 南丰县| 绩溪县| 张家港市| 徐水县| 中超| 惠来县| 邢台市| 富源县| 泽州县| 嘉禾县| 朝阳区| 洞头县| 颍上县| 黄梅县| 鄂伦春自治旗| 喜德县| 合阳县| 吉木乃县| 盘山县| 女性| 蓬溪县| 灵川县| 浦江县| 中宁县| 宜良县| 鄂托克旗| 建湖县| 南陵县| 莲花县|