【问题标题】:Parsing XML in laravel在 laravel 中解析 XML
【发布时间】:2016-11-15 17:13:09
【问题描述】:

我试图生成一个 xml,但我得到一个错误:FatalErrorException in 6955f07d83b93bb8aa89577b116866e228e0c155.php line 1 syntax error, unexpected 'version' (T_STRING).

我无法弄清楚我在代码中做错了什么。

我的控制器功能是:

public function feed($gallery_id){
        $products = Products::select("id","title","brand","url","path")->where("gallery_id",$gallery_id)->get();

        foreach ($products as $product) {
            $product->path = url($product->path);
        }
        return response()->view('xml.gallery', compact('products'))->header('Content-Type', 'text/xml');

    }

我的刀片(xml.gallery):

    <?xml version="1.0"?>
    <rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">
      <channel>
        <title>Test Store</title>
        <link>http://domain.com</link>
        <description>An example item from the feed</description>
       @​foreach($products as $product)
          <item>
          <g:id>1</g:id>
          <g:title>something</g:title>
          <g:description>Solid plastic Dog Bowl in marine blue color</g:description>
          <g:link>http://www.zara.com</g:link>
          <g:image

_link>http://domain.com/images/photos_gallery/14788772681.png</g:image_link>
      <g:price>12 GBP</g:price>
      <g:brand>Nike</g:brand>
       <g:availability>in stock</g:availability>
       <g:condition>new</g:condition>
    </item>
  @​endforeach
  </channel>
</rss>

【问题讨论】:

    标签: php xml laravel


    【解决方案1】:

    您的short_open_tag 似乎已启用。它告诉 PHP 是否应该允许 PHP 的开放标签的短格式 (&lt;? ?&gt;)。如果您想将 PHP 与 XML 结合使用,您可以禁用此选项以便使用 &lt;?xml ?&gt; 内联。

    但其他简单的解决方案是在您的视图文件中编写以下代码:

    <?php echo '<?xml version="1.0"?>'; ?>
    

    【讨论】:

      【解决方案2】:

      我们解决它的方法是将 xml 标头存储为变量,然后将其传递:

      $xml_version = '<?xml version="1.0"?>';
      
      return response()->view('xml.gallery', compact('products', 'xml_version'))->header('Content-Type', 'text/xml');
      

      然后您可以将其放入刀片中:

      {{$xml_version}}
      

      【讨论】:

        猜你喜欢
        • 2016-11-17
        • 1970-01-01
        • 2019-09-18
        • 2018-09-08
        • 1970-01-01
        • 2019-11-25
        • 1970-01-01
        • 2012-08-11
        • 1970-01-01
        相关资源
        最近更新 更多