Pedro
Pedro

Reputation: 1477

Parsing XML in laravel

im trying to generate a xml but im getting a error: FatalErrorException in 6955f07d83b93bb8aa89577b116866e228e0c155.php line 1 syntax error, unexpected 'version' (T_STRING).

I cant figure out what im doing wrong in my code.

My controller function is:

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');

    }

My blade (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>

Upvotes: 4

Views: 3892

Answers (3)

Bipu
Bipu

Reputation: 21

Try this for return XML response from the Laravel blade file. Cloud host like the digital ocean doesn't need this, you can return XML as HTML tags. But cPanel hosting shows a syntax error. So echo the XML start tag. I will work.

<?php 
echo '<?xml version="1.0" encoding="utf-8" ?>'; ?>

Upvotes: 1

Amit Gupta
Amit Gupta

Reputation: 17658

It looks like your short_open_tag is enabled. It tells PHP whether the short form (<? ?>) of PHP's open tag should be allowed. If you want to use PHP in combination with XML, you can disable this option in order to use <?xml ?> inline.

But other easy solution will be to write following code in your view file:

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

Upvotes: 13

aynber
aynber

Reputation: 23010

The way we've gotten around it is to store the xml header as a variable, then pass it along:

$xml_version = '<?xml version="1.0"?>';

return response()->view('xml.gallery', compact('products', 'xml_version'))->header('Content-Type', 'text/xml');

Which you can then place into your blade:

{{$xml_version}}

Upvotes: 8

Related Questions