PHP header issue - php

I have set HTTP header in PHP as:
header("Content-Type: application/xml");
And I got following response:
<?xml version="1.0" encoding="utf-8"?>
<feedback>
<result>False</result>
</feedback>
But when I check headers, it says the response type is application/atom+xml . I need this in application/xml format. What could be the reason for this issue?

There's no reason why that shouldn't work. I've just tried a simple test case with that xml and it certainly works for me.
<?php
header("Content-Type: application/xml");
?><?xml version="1.0" encoding="utf-8"?>
<feedback>
<result>False</result>
</feedback>
Either there is more to your php that you're not showing us, or possibly there is something misconfigured in your server software.
Update
Based on the information you provided in your answer, I'd say you could fix this by changing your root element to anything that doesn't start with <feed. As I said in my comment, something is incorrectly interpreting this as being an Atom feed and rewriting the content-type.
That said, there are assumedly other strings (e.g. <rss) that might trigger other rewrites of the content-type, so it would be preferable if you could track down whatever system was responsible for the error and get rid of it.

I can't say what the technical explanation. But changing the XML content fixed my issue. It was a slight change. I just added a top level branch element,
<?xml version="1.0" encoding="utf-8"?>
<data>
<feedback>
<result>True</result>
</feedback>
</data>

Related

Pretty print curl raw requests from saved files

I have a set of requests and responses saved in files as raw petitions.
The files are like this:
--MIME_Boundary
Content-ID: <root.message#cxf.apache.org>
Content-Type: application/xop+xml; type="text/xml"; charset=utf-8
Content-Transfer-Encoding: 8bit
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Header/><soap:Body xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><ns2:LoadFileResponse xmlns:ns2="http://ws.loadfile.com/loadfile/ws/LoadFileService" xmlns:ns3="http://ws.loadfile.com/loadfile/ws/CheckFileService"><ns2:responseFile><ns2:status>wait</ns2:status><ns2:fileId>2356</ns2:fileId></ns2:responseFile></ns2:LoadFileResponse></soap:Body></soapenv:Envelope>
--MIME_Boundary--
I want to show in a blade template with syntax highlight and code formatted. I used highlight.js to show the colors in the code, but I don´t know how to reformat the code.
I need something like codebeautify.org (https://codebeautify.org/xmlviewer/cbf69d49) but inside my own code.
I tried using DomDocument, but it´s not strictly XML and throws exceptions when using loadXML.
Is there any class or package to help me?
Thank you!

PHP - create VLC Playlist

I'm currently trying to create a VLC playlist with PHP. This works pretty well with SimpleXML, but on one point I'm stuck.
A VLC Playlist needs a starting tag like:
<?xml version="1.0" encoding="UTF-8"?>
But Simple XML is always creating a xml tag around the whole element, which will look like
<xml>
<tag></tag>
</xml>
But what i need is:
<?xml version="1.0" encoding="UTF-8"?>
<playlist xmlns="[...]" xmlns:vlc="[...]" version="1">
<title>Test<title>
<trackList>
[...]
</trackList>
</playlist>
How can i create a xml file like this with simpleXML?
You miss some specifics about XML which drives you into wrong assumptions. Let's shed some light:
A VLC Playlist needs a starting tag like:
<?xml version="1.0" encoding="UTF-8"?>
This is wrong in many ways. First of all this is not a starting tag. This is the so called XML Declaration. So it is not a starting tag but something different.
Second, VLC does not require this. The XML Declaration is optional so it is for VLC.
But Simple XML is always creating a xml tag around the whole element, which will look like
No, Simple XML is handling this fine. It does not create a xml tag around the whole element. What you did do actually was wrong, resulting into the wrong results. But that is not SimpleXML's fault in this case but your fault (you have not shared the code, so excuse the generalization I do here).
Then you ask how to create some XML file with SimpleXML. With the (little) information you've shared, it goes like this:
$playlist = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><playlist xmlns="uri:1" xmlns:vlc="uri:2" version="1"/>');
$playlist->title = 'Test';
$playlist->trackList = "[...]";
$playlist->asXML('php://output');
Output then is:
<?xml version="1.0" encoding="utf-8"?>
<playlist xmlns="uri:1" xmlns:vlc="uri:2" version="1"><title>Test</title><trackList>[...]</trackList></playlist>
If you want it pretty-printed, see here: PHP simpleXML how to save the file in a formatted way?
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><playlist></playlist>');
$trackList = $xml->addChild('trackList');
foreach ($_POST['files'] as $video) {
$track = $trackList->addChild('track');
$track->addChild('location', 'file://'.$tmp_dir.'/'.$video['path']);
$track->addChild('title', $video['name']);
}
file_put_contents('playlist.xspf',$xml->asXML());

php , Generating Sitemap Xml on the fly [duplicate]

Any ideas on how I can get PHPs SimplXMLElement to kick off with the following?
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.2">
The main root will then be:
<Document></Document>
Or do I use simplexml_load_string() to set it up?
Context: I am extending simpleXmlElement to create some kml files.
EDIT
Actually, setting the kml xmlns was laughably easy to do:
new simpleXMLElement('<kml xmlns="http://earth.google.com/kml/2.2">
<Document></Document></kml>');
Just how to set encoding="UTF-8" that is bothering me, seemingly the kml is acceptable without that, but I'd still like to understand how to do it if pos.
new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?>'
.'<kml xmlns="http://earth.google.com/kml/2.2">'
.'<Document></Document></kml>');

SimpleXml how to correctly set encoding and xmins?

Any ideas on how I can get PHPs SimplXMLElement to kick off with the following?
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.2">
The main root will then be:
<Document></Document>
Or do I use simplexml_load_string() to set it up?
Context: I am extending simpleXmlElement to create some kml files.
EDIT
Actually, setting the kml xmlns was laughably easy to do:
new simpleXMLElement('<kml xmlns="http://earth.google.com/kml/2.2">
<Document></Document></kml>');
Just how to set encoding="UTF-8" that is bothering me, seemingly the kml is acceptable without that, but I'd still like to understand how to do it if pos.
new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?>'
.'<kml xmlns="http://earth.google.com/kml/2.2">'
.'<Document></Document></kml>');

My PHP code to produce XML document is not working right

I want to create/produce XML document from PHP, but not working.
I have this code right here:
<?php
$xml = new SimpleXMLElement("<evalues></evalues>");
while ($row = mysqli_fetch_array($result)){
$evalue = $xml->addChild('evalue', $row[1]);
$evalue->addAttribute('id', $row[0]);
}
echo $xml->saveXML();
?>
the result looks like this, in one long line
1513971901.549931756795512.970022842372553.270163414774046.390384216874570.370032821081734.920144539784.98
The source code looks like this, which is correct, but I want it to look like below in the browser as an XML document without having to view the source code.
<?xml version="1.0"?>
<evalues>
<evalue id="5">1513971901.54993</evalue>
<evalue id="6">1756795512.97002</evalue>
<evalue id="7">2842372553.27016</evalue>
<evalue id="8">3414774046.39038</evalue>
<evalue id="9">4216874570.37003</evalue>
<evalue id="10">2821081734.92014</evalue>
<evalue id="11">4539784.98</evalue>
</evalues>
Right now when i right click on the page and click Save Page As, it shows .htm at the end BUT i want it to show .xml
Just give your client a proper content type:
header('Content-Type: text/xml');
You should set the header content type to text/xml so that the browser could identify that the document is an xml document.
header('Content-Type: text/xml');
You should put this code before anything is outputted to the document. That is before any echo of print statement. In this case you case put the code before the loop.
Since there is no stylesheet attached to your xml document Internet Explorer may still display it as a plain text file. You can see the document as it is using Mozilla Firefox.
If you don't want to actually use the XML (just display it), change your last line to
echo '<pre>', htmlspecialchars($xml->saveXML()), '</pre>';

Categories