2

I produced xml file in my directory. I want to show this file in browser using php?

(I want to show that as sitemap)

Here is my code :

   public function siteMap()
    {
        $test_array = array (
            'bla' => 'blub',
            'foo' => 'bar',
            'another_array' => array (
                'stack' => 'overflow',
            ),
        );

        $xml_template_info = new \SimpleXMLElement("<?xml version=\"1.0\"?><template></template>");

        $this->array_to_xml($test_array,$xml_template_info);
        $xml_template_info->asXML(dirname(__FILE__)."/sitemap.xml") ;
        header('Content-type: text/xml');
        dd(readfile(dirname(__FILE__)."/sitemap.xml"));
    }

    public function array_to_xml(array $arr, \SimpleXMLElement $xml)
    {
      foreach ($arr as $k => $v) {
          is_array($v)
              ? $this->array_to_xml($v, $xml->addChild($k))
              : $xml->addChild($k, $v);
      }
      return $xml;
   }

And here is sitemap.xml:

<?xml version="1.0"?>
<template><bla>blub</bla><foo>bar</foo><another_array><stack>overflow</stack></another_array></template>

Here is result : enter image description here

I want show this xml file in my browser, Any idea for doing this correctly?

EDITED : Here is result with header('Content-type: text/plain'); enter image description here

It consist of some extra reports.

7
  • 1
    What is the current behavior? Commented Jul 13, 2016 at 7:16
  • 1
    I think header('Content-type: text/plain'); should work! Commented Jul 13, 2016 at 7:20
  • @Alok I added to my question. Commented Jul 13, 2016 at 7:44
  • @BenHillier I added it's result to my question. Commented Jul 13, 2016 at 7:50
  • That error is telling you exactly what's wrong with the output. There's a lot of extra content after the </template> tag is closed. Nothing will display it as XML, because it's not valid XML. Something is sending the extra data! Perhaps try an exit(); after the readfile call. Commented Jul 13, 2016 at 8:26

10 Answers 10

8

Assuming that the content of xml is in $xml variable, you can use one of the followings:

return response($xml, 200)->header('Content-Type', 'application/xml');

OR

return response($xml, 200, ['Content-Type' => 'application/xml']);
Sign up to request clarification or add additional context in comments.

Comments

6

In your view create sitemap.blade.php.
Insert content xml in your sitemap.blade.php.
Create a function called sistemap.

 public function sitemap(){

     return response()->view('sitemap')->header('Content-Type', 'text/xml');

 }


In your routes:

 Route::get('sitemap', function () {
     return view('sitemap');
 });

Comments

2

Change your route to this:

Route::get('/sitemap.xml', function (Request $request) {
     return response()->view('sitemap')->header('Content-Type', 'text/xml');
});

Comments

1

Try:

<?php

public function array_to_xml(array $data, \SimpleXMLElement $xml) {
    foreach ($data as $key => $value) {
        if (is_array($value)) {
            if (is_numeric($key)) {
                $key = 'item' . $key; //dealing with <0/>..<n/> issues
            }
            $subnode = $xml->addChild($key);
            $this->array_to_xml($value, $subnode);
        } else {
            $xml->addChild("$key", htmlspecialchars("$value"));
        }
    }
}

public function siteMap()
    {
$data = array(
        'bla' => 'blub',
        'foo' => 'bar',
        'another_array' => array(
            'stack' => 'overflow',
        ),
    );

    $xml_template_info = new \SimpleXMLElement("<?xml version=\"1.0\"?><template></template>");
    $this->array_to_xml($data, $xml_template_info);
    $xml_template_info->asXML(dirname(__FILE__) . "/sitemap.xml");
    header('Content-type: text/xml');
    die(readfile(dirname(__FILE__) . "/sitemap.xml"));
//or return readfile(dirname(__FILE__) . "/sitemap.xml");
    }

4 Comments

I'm coding in Laravel so I must use $this->array_to_xml($test_array, $xml_template_info);
So implement that function to your method in Laravel, you not give body of your method so I add simple one function.
I did it , when we use die() , it shows me proper result. but is there any way to return it's result as xml?
Also , FILE is undefined constant so when I put FILE i get 127.
1

That error is telling you exactly what's wrong with the output. There's a lot of extra content after the </template> tag is closed. Nothing will display it as XML, because it's not valid XML. Something is sending the extra data!

Perhaps try an exit(); after the readfile call.

Comments

0

If you want to just show the contents of an xml file in browser same way as they look in xml file,just write name_of_xml_file.xml in url after the base address.

For example:

www.example.com/name_of_xml_file.xml

regardless of where you place it.

It should be in your websites folder and just use a function to redirect to that page if you want to autoload when a certain thing happens.

Comments

0

What I did was add the xml file in the public folder of Laravel. Something like sitemap.xml. So you can easily access it with http://yourdomain.com/sitemap.xml.

Comments

0

Laravel's dd function outputs a bunch of HTML to let you browse the debugging input. It's intended for debugging, not outputting normal stuff to the page. You shouldn't be using it like this.

Instead of

dd(readfile(dirname(__FILE__)."/sitemap.xml"));

try

return readfile(dirname(__FILE__)."/sitemap.xml");

Comments

0

I created a Laravel xml response package: https://github.com/mtownsend5512/response-xml

Install that and it's as simple as:

$test_array = array (
        'bla' => 'blub',
        'foo' => 'bar',
        'another_array' => array (
            'stack' => 'overflow',
        ),
    );
return response()->xml($test_array, 200, [], 'template');

And you get:

<?xml version="1.0"?>
<template>
    <bla>blub</bla>
    <foo>bar</foo>
    <another_array>
        <stack>overflow</stack>
    </another_array>
</template>

Comments

0

Create a route:

Route::get('sitemap.xml', 'SitemapController@index')->name('sitemapxml'); 

Put this code in your controller:

public function index() {
    $posts = Post::where('status', '=', 1)->get();
    return response()->view('sitemap_xml', ['posts' => $posts])->header('Content-Type', 'text/xml');
}

Put this code in your view file:

<?= '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
        http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
    @foreach ($posts as $post)
        <url>
            <loc>{{ url($post->url) }}</loc>
            <lastmod>{{ $post->updated_at->tz('UTC')->toAtomString() }}</lastmod>
            <changefreq>monthly</changefreq>
            <priority>0.7</priority>
        </url>
    @endforeach
</urlset>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.