温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

laravel中怎么输出xml数据

发布时间:2021-07-19 13:58:23 来源:亿速云 阅读:405 作者:Leah 栏目:编程语言

laravel中怎么输出xml数据,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

laravel框架怎么返回xml格式数据?

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

这样的话是没有效果的,会提示这样的错误:

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的时候会自行用text/html方式返回数据,解决办法:

需要return response($xml,200)->header(“Content-type”,“text/xml”);这样的方式才能改变header头

laravel返回xml数据格式例子:

/**
  * 神马搜索数据结构化,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");
    }
 /**
  * 神马搜索数据结构化,节点的具体内容 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格式的数据直接加上 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;
//创建XML单项
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;
}
?>

看完上述内容,你们掌握laravel中怎么输出xml数据的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI