背景:

seo的同事要批量提交xml格式的数据到搜索引擎,目前项目用laravel框架开发的,所以就有了这篇文章的诞生了。网上有不少关于php输出xml格式的例子,小弟不才也搬过,只是在php文件上面测试是没问题的,把它搬到laravel框架里面,就有有坑了,主要原因是header头的问题。

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

更多PHP相关知识,请访问PHP教程!

更多相关文章

  1. 新手入门PHP必知的七种数据类型
  2. PHP+jQuery实现中国地图热点数据统计展示效果
  3. PHP生成器yield处理大量数据业务(代码示例)
  4. php实现将表单内容提交到数据库
  5. 干货丨时序数据库DolphinDB历史数据回放教程
  6. 数据库迁移搞炸了!没用这款开源神器的锅?
  7. MySQL如何实时同步数据到ES?试试这款阿里开源的神器!
  8. 如何离线更新vSAN数据库
  9. IDEA同款数据库管理工具,提示太全了,用起来贼香!

随机推荐

  1. android webview ERR_UNKNOWN_URL_SCHEME
  2. Android模拟、实现、触发系统按键事件的
  3. android中之断点续传
  4. 学习Android开发的好教程
  5. Android通过编码实现GPS开关
  6. Android P sensor对某个APP方向做旋转
  7. android 8.0、8.1 崩溃报 "Only fullscre
  8. android下对xml的解析
  9. 关于android Error:Execution failed for
  10. android 加密 解密