当前位置:首页 > CMS教程 > 其它CMS > 列表

laravel输出xml数据,php输出xml格式数据

发布:smiling 来源: PHP粉丝网  添加日期:2022-05-23 09:02:26 浏览: 评论:0 

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

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

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

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

  1. This page contains the following errors: 
  2.  
  3. error on line 14 at column 6: XML declaration allowed only at the start of the document 
  4.  
  5. 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数据格式例子:

  1. /** 
  2.  
  3.   * 神马搜索数据结构化,written:yangxingyi Data:2018-10-25 11:15 
  4.  
  5.   */ 
  6.  
  7.  public function index(Request $request){ 
  8.  
  9.         $data_array = array( 
  10.  
  11.             array( 
  12.  
  13.                 'title' => 'title1', 
  14.  
  15.                 'content' => 'content1', 
  16.  
  17.                 'pubdate' => '2009-10-11', 
  18.  
  19.             ), 
  20.  
  21.             array( 
  22.  
  23.                 'title' => 'title2', 
  24.  
  25.                 'content' => 'content2', 
  26.  
  27.                 'pubdate' => '2009-11-11', 
  28.  
  29.             ) 
  30.  
  31.         ); 
  32.  
  33.         $title_size = 1; 
  34.  
  35.         $xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"; 
  36.  
  37.         $xml .= "<article>\n"; 
  38.  
  39.         foreach ($data_array as $data) { 
  40.  
  41.             $xml .= $this->create_item($data['title'], $title_size, $data['content'], $data['pubdate']); 
  42.  
  43.         } 
  44.  
  45.         $xml .= "</article>\n"; 
  46.  
  47.         #echo $xml; 
  48.  
  49.         return response($xml,200)->header("Content-type","text/xml"); 
  50.  
  51.     } 
  52.  
  53.  /** 
  54.  
  55.   * 神马搜索数据结构化,节点的具体内容 written:yangxingyi 
  56.  
  57.   */ 
  58.  
  59.     private function create_item($title_data, $title_size, $content_data, $pubdate_data) 
  60.  
  61.     { 
  62.  
  63.         $item = "<item>\n"; 
  64.  
  65.         $item .= "<title size=\"" . $title_size . "\">" . $title_data . "</title>\n"; 
  66.  
  67.         $item .= "<content>" . $content_data . "</content>\n"; 
  68.  
  69.         $item .= " <pubdate>" . $pubdate_data . "</pubdate>\n"; 
  70.  
  71.         $item .= "</item>\n"; 
  72.  
  73.         return $item; 
  74.  
  75.     } 

PHP生成xml格式的数据直接加上 header(“Content-type: text/xml”);头就行了

  1. <?php 
  2.  
  3.  header("Content-type: text/xml"); 
  4.  
  5. $data_array = array( 
  6.  
  7.     array( 
  8.  
  9.     'title' => 'title1', 
  10.  
  11.     'content' => 'content1', 
  12.  
  13.         'pubdate' => '2009-10-11', 
  14.  
  15.     ), 
  16.  
  17.     array( 
  18.  
  19.     'title' => 'title2', 
  20.  
  21.     'content' => 'content2', 
  22.  
  23.     'pubdate' => '2009-11-11', 
  24.  
  25.     ) 
  26.  
  27. ); 
  28.  
  29. $title_size = 1; 
  30.  
  31. $xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"; 
  32.  
  33. $xml .= "<article>\n"; 
  34.  
  35. foreach ($data_array as $data) { 
  36.  
  37. $xml .= create_item($data['title'], $title_size, $data['content'], $data['pubdate']); 
  38.  
  39. } 
  40.  
  41. $xml .= "</article>\n"; 
  42.  
  43. echo $xml; 
  44.  
  45. //创建XML单项 
  46.  
  47. function create_item($title_data, $title_size, $content_data, $pubdate_data) 
  48.  
  49. { 
  50.  
  51.     $item = "<item>\n"; 
  52.  
  53.     $item .= "<title size=\"" . $title_size . "\">" . $title_data . "</title>\n"; 
  54.  
  55.     $item .= "<content>" . $content_data . "</content>\n"; 
  56.  
  57.     $item .= " <pubdate>" . $pubdate_data . "</pubdate>\n"; 
  58.  
  59.     $item .= "</item>\n"; 
  60.  
  61.     return $item; 
  62.  
  63. } 
  64.  
  65. ?>

Tags: laravel输出xml php输出xml

分享到: