laravel中怎么輸出xml數(shù)據(jù),相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。
創(chuàng)新互聯(lián)致力于互聯(lián)網(wǎng)網(wǎng)站建設(shè)與網(wǎng)站營(yíng)銷(xiāo),提供成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、網(wǎng)站開(kāi)發(fā)、seo優(yōu)化、網(wǎng)站排名、互聯(lián)網(wǎng)營(yíng)銷(xiāo)、小程序開(kāi)發(fā)、公眾號(hào)商城、等建站開(kāi)發(fā),創(chuàng)新互聯(lián)網(wǎng)站建設(shè)策劃專(zhuān)家,為不同類(lèi)型的客戶(hù)提供良好的互聯(lián)網(wǎng)應(yīng)用定制解決方案,幫助客戶(hù)在新的全球化互聯(lián)網(wǎng)環(huán)境中保持優(yōu)勢(shì)。
laravel框架怎么返回xml格式數(shù)據(jù)?
如果用header(“Content-type: text/xml”);
這樣的話(huà)是沒(méi)有效果的,會(huì)提示這樣的錯(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í)候會(huì)自行用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; } ?>
看完上述內(nèi)容,你們掌握l(shuí)aravel中怎么輸出xml數(shù)據(jù)的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
網(wǎng)站標(biāo)題:laravel中怎么輸出xml數(shù)據(jù)
網(wǎng)頁(yè)URL:http://bm7419.com/article28/iioocp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、網(wǎng)站建設(shè)、品牌網(wǎng)站設(shè)計(jì)、商城網(wǎng)站、品牌網(wǎng)站建設(shè)、軟件開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
移動(dòng)網(wǎng)站建設(shè)知識(shí)