0

im trying to grab a xml file from my storage and than show it on a url, but is not working, the page is blank, the issue is not in the route, it must me in my code, can someone tell me where is the problem?

Code:

$xml = simplexml_load_file(storage_path('app\file.xml'));
return \Response::make($xml , '200')->header('Content-Type', 'text/xml');
Pedro
  • 1,459
  • 6
  • 22
  • 40

1 Answers1

0

\Response::make expects you to pass a string with the content you want to return, but you are passing an object: simplexml_load_file returns a SimpleXMLElement instance.

As it happens, a SimpleXMLElement can be converted to a string, but it won't return the full XML, just the content of that element - it's so that you can do things like $xml = simplexml_load_string('<doc><message>Hello, World!</message></doc>); $message = (string)$xml->message;

If you just want the full contents of the XML file, without any changes, you don't need to parse it with SimpleXML at all, just load the contents into a string:

$xml_string = file_get_contents(storage_path('app\file.xml'));
return \Response::make($xml_string , '200')->header('Content-Type', 'text/xml');

If the idea is that you'll be manipulating it later, so you want the object, then you need to call ->asXML() to turn it back into a string:

$xml = simplexml_load_file(storage_path('app\file.xml'));
#TODO: Do something interesting with $xml
$xml_string = $xml->asXML();
return \Response::make($xml_string , '200')->header('Content-Type', 'text/xml');
IMSoP
  • 89,526
  • 13
  • 117
  • 169