Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHPLaravel-如何在浏览器中显示xml文件_Php_Xml_Laravel - Fatal编程技术网

PHPLaravel-如何在浏览器中显示xml文件

PHPLaravel-如何在浏览器中显示xml文件,php,xml,laravel,Php,Xml,Laravel,我在我的目录中生成了xml文件。我想用php在浏览器中显示这个文件 我想把它显示为站点地图 这是我的密码: public function siteMap() { $test_array = array ( 'bla' => 'blub', 'foo' => 'bar', 'another_array' => array ( 'stack' =&

我在我的目录中生成了xml文件。我想用php在浏览器中显示这个文件

我想把它显示为站点地图

这是我的密码:

   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;
   }
下面是sitemap.xml:

结果如下:

我想在我的浏览器中显示这个xml文件,有没有正确的方法

编辑:这里是标题为“内容类型:文本/普通”的结果;

它包括一些额外的报告。

试试:

<?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");
    }
尝试:


这个错误确切地告诉您输出出了什么问题。标记关闭后会有很多额外的内容。没有任何内容将其显示为XML,因为它不是有效的XML。有东西正在发送额外的数据


也许尝试一个出口;在readfile调用之后

该错误确切地告诉您输出出了什么问题。标记关闭后会有很多额外的内容。没有任何内容将其显示为XML,因为它不是有效的XML。有东西正在发送额外的数据


也许尝试一个出口;在readfile调用之后

如果您想在浏览器中以查看xml文件的相同方式显示xml文件的内容,只需在url中的基址后写上\u xml\u file.xml的名称

例如:

www.example.com/name_of_xml_file.xml

不管你把它放在哪里


它应该在你的网站文件夹中,如果你想在某件事情发生时自动加载,只需使用一个函数重定向到该页面。

如果你想在浏览器中以与在xml文件中相同的方式显示xml文件的内容,只需在url中的基本地址后写上xml文件的名称即可

例如:

www.example.com/name_of_xml_file.xml

不管你把它放在哪里


它应该在您的网站文件夹中,如果您想在发生特定事件时自动加载,只需使用一个函数重定向到该页面。

在您的视图中创建sitemap.blade.php。 在sitemap.blade.php中插入内容xml。 创建一个名为sistemap的函数

 public function sitemap(){

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

 }
在您的路线中:


在您的视图中创建sitemap.blade.php。 在sitemap.blade.php中插入内容xml。 创建一个名为sistemap的函数

 public function sitemap(){

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

 }
在您的路线中:


我所做的是在Laravel的公共文件夹中添加xml文件。类似于sitemap.xml的东西。因此,您可以使用http://yourdomain.com/sitemap.xml.

我所做的是在Laravel的公用文件夹中添加xml文件。类似于sitemap.xml的东西。因此,您可以使用http://yourdomain.com/sitemap.xml.

Laravel的dd函数输出一组HTML,让您浏览调试输入。它用于调试,而不是将普通内容输出到页面。你不应该这样使用它

而不是

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

Laravel的dd函数输出一组HTML,让您浏览调试输入。它用于调试,而不是将普通内容输出到页面。你不应该这样使用它

而不是

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


我创建了一个Laravel xml响应包:

安装它,操作非常简单:

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

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

我创建了一个Laravel xml响应包:

安装它,操作非常简单:

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

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

假设xml的内容在$xml变量中,您可以使用以下选项之一:

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


假设xml的内容在$xml变量中,您可以使用以下选项之一:

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


将您的路线更改为:

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

将您的路线更改为:

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

当前的行为是什么?我认为标题是'Content-type:text/plain';应该有用@我在我的问题中添加了Alok。@BenHillier我在我的问题中添加了它的结果。这个错误正告诉您输出的错误。标记关闭后会有很多额外的内容。没有任何内容将其显示为XML,因为它不是有效的XML。有东西正在发送额外的数据!也许尝试一个出口;在readfile调用之后。当前的行为是什么?我认为标题'Content-type:text/plain';应该有用@我在我的问题中添加了Alok。@BenHillier我在我的问题中添加了它的结果。这个错误正告诉您输出的错误。标记关闭后会有很多额外的内容。没有任何内容将其显示为XML,因为它不是有效的XML。有东西正在发送额外的数据!也许尝试一个出口;在readfile调用之后。我正在用Laravel编码,所以我必须使用$this->array\u to\uXML$test\u array,$xml\u template\u info;所以在Laravel中实现这个函数到你们的方法中,你们并没有给出你们的方法的主体,所以我添加了一个简单的函数。我做了,当我们使用die时,它向我展示了正确的结果。但有没有办法将其结果返回为xml?另外,文件是未定义的常量,所以当我放入文件时,我得到127。我用Laravel编码,所以我必须使用$this->array\u to\u xml$test\u array,$xml\u template\u info;所以在Laravel中实现这个函数到你们的方法中,你们并没有给出你们的方法的主体,所以我添加了一个简单的函数。我做了,当我们使用die时,它向我展示了正确的结果。但是有没有办法把它还给我呢
同样,文件是未定义的常量,所以当我放置文件时,我得到127。