Home  >  Article  >  Backend Development  >  laravel outputs xml data, php outputs xml format data

laravel outputs xml data, php outputs xml format data

藏色散人
藏色散人forward
2020-01-15 14:47:253386browse

Background:

A colleague from seo needs to submit data in xml format to the search engine in batches. The current project is developed using the laravel framework, so this article was born. There are many examples on the Internet about PHP outputting XML format. I have also moved it before. It is no problem to just test it on the PHP file. If I move it to the Laravel framework, there will be pitfalls. The main reason is the header header. question.

How does the laravel framework return xml format data?

If you use header(“Content-type: text/xml”);

this will have no effect and an error like this will be prompted:

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.

The laravel framework will return data in text/html mode when outputting xml. Solution:

requires return response($xml,200)->header ("Content-type", "text/xml"); This way can change the header header

laravel returns xml data format example:

/**
  * 神马搜索数据结构化,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[&#39;title&#39;], $title_size, $data[&#39;content&#39;], $data[&#39;pubdate&#39;]);
        }
        $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 generates data in xml format and adds header("Content-type: text/xml"); header.

<?php
 header("Content-type: text/xml");
$data_array = array(
    array(
    &#39;title&#39; => &#39;title1&#39;,
    &#39;content&#39; => &#39;content1&#39;,
        &#39;pubdate&#39; => &#39;2009-10-11&#39;,
    ),
    array(
    &#39;title&#39; => &#39;title2&#39;,
    &#39;content&#39; => &#39;content2&#39;,
    &#39;pubdate&#39; => &#39;2009-11-11&#39;,
    )
);
$title_size = 1;
$xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
$xml .= "<article>\n";
foreach ($data_array as $data) {
$xml .= create_item($data[&#39;title&#39;], $title_size, $data[&#39;content&#39;], $data[&#39;pubdate&#39;]);
}
$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;
}
?>

For more PHP related knowledge, please visitPHP tutorial!

The above is the detailed content of laravel outputs xml data, php outputs xml format data. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete