How to wrap my rss feed with asXML function - php

I have a page called rss.php that contains PHP SQL and XML which dynamically, and might I add PERFECTLY, produces the XML needed for the rss feed for my podcast. Only problem is, its a PHP file. I need to get the stuff this page spits out in to a file named rss.xml for iTunes to accept it. I came across this little bit of code in another thread:
echo $xml->asXML('filename.xml');
and this on http://php.tonnikala.org:
<?php
$string = <<<XML
<a>
<b>
<c>text</c>
<c>stuff</c>
</b>
<d>
<c>code</c>
</d>
</a>
XML;
$xml = new SimpleXMLElement($string);
echo $xml->asXML(); // <?xml ... <a><b><c>text</c><c>stuff</c> ...
?>
Problem is when I wrap my XML in this code, the browser gives me an error and it doesn't load. This may be because my XML isn't just XML, it's PHP and SQL. And now that I think about it, maybe it's cause my PHP hasn't been processed by the server yet...
Anyway, what I want to do is get this rss.php page to spit out XML and save it to a file called rss.xml. Also, can I control how often this happens? Or will it happen every time the page loads?

The answer to my question turned out to be that I DIDN'T need the page to be an XML document at all. As long as the declaration at the beginning says it's XML, its fine. Here's what I did. Remember place this at the VERY TOP of the PHP page.
<?php echo('<?xml version="1.0" encoding="UTF-8"?>') ?>

Related

php xml DOMDocument close tag element

I am using PHP DOMDocument() to generate XML file with elements.
I am appending all details into sample xml file into components tag. But closing tag is not coming. I want to create closing tag.
My Code is doing this
<component expiresOn="2022-12-31" id="pam" />
I want to do like following
<component expiresOn="2022-12-31" id="pam"></component>
My PHP CODE SAMPLE
$dom = new DOMDocument();
$dom->load("Config.xml");
$components = $dom->getElementsByTagName('components')->item(0);
if(!empty($_POST["pam"])) {
$pam = $_POST["pam"];
$component = $dom->createElement('component');
$component->setAttribute('expiresOn', $expirydate);
$component->setAttribute('id', "pam");
$components->appendChild($component5);
}
$dom->save("Config.xml");
I tested following suggestion and its not working. Both xml-php code are different.
$dom->saveXml($dom,LIBXML_NOEMPTYTAG);
Self-closing tags using createElement
I tested following.
You're trying to use DOMDocument::saveXML to save the new XML back into the original file, but all that function does is return the XML as a string. Since you aren't assigning the result to anything, nothing happens.
If you want to save the XML back to your file, as well as avoiding self-closing tags, you'll need to use the save method as you originally were, and also pass the option:
$dom->save('licenceConfig.xml', LIBXML_NOEMPTYTAG);
See https://3v4l.org/e6N5s for a demo

Pass PHP variable as a parameter to XSL

I have a PHP page that outputs XML by changing the header type and outputting the xml with an XSL stylesheet for an RSS feed:
<?php
header('Content-Type: text/xml; charset=utf-8');
echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
echo '<?xml-stylesheet title="XSL_formatting" type="text/xsl" href="/path/feed.xsl"?>' . "\n";
?>
<channel>
<item>...</item>...
</channel>
I want to pass some PHP variables from the original page to the XSL, how do I do this?
Example:
I have the variables...
$header = "This is a cool page";
$description = "This is a description";
...that I want to pass to the XSL page and use within it meaning the title can be changed through PHP and dynamically changed in the XSL rather than hard coding it.
It cannot be passed through XML because I am using the XSL as a fallback and therefore the title should not be displayed when the browser supports RSS. Also it must come from that page rather than referencing another file with that variable.
Something like
<?xml-stylesheet title="XSL_formatting" type="text/xsl" param-header="<?=$header?>' param-description="<?=$description?>' href="/path/feed.xsl"?>
Let me know if there is a better way to achieve this.
*UPDATE - This doesn't work because the extra tags don't validate because they aren't part of a namespace.
Actually forgot to put the rss tags into the question but when I added XML to store the PHP variables between the rss and channel tags, I could use them in the XSL file dynamically and the XML used to store them doesn't render when viewed in an RSS reader, e.g.
<rss>
<extraInfo>
<heading>...</heading>
<description>...</description>
</extraInfo>
<channel>
...
</channel>
</rss>

PHP in XML file (or PHP file as XML one)

I have this code (part of bigger script):
flashvars.xmlSource = "datasource.xml";
datasource.xml looks like:
<?xml version="1.0" encoding="utf-8"?>
<Object>
<Contents>
<Source="address" Title="title"></Source>
<Description><h1>New hot Features</h1><p>The all new Piecemaker comes with lots of new features, making it even more slick.</p><p>Just to mention a few - you can now specify unlimited transition styles, include your own SWF and Video files, add hyperlinks to images and info texts with all special characters.</p><p>We also impoved the navigation and the animation with animated shadows and pixel-perfect transitions.</p></Description>
(...)
</Contents>
</Object>
I want to generate datasource.xml dynamically using foreach loop.
I've just changed the file extension to .php but this is not that easy ;)
Any ideas?
Funny or not, but try this one:
leave your file extension to be "xml"
where you wrote (...) write <? PHP CODE HERE ?>
So handle it as if it would be some html file. What I mean is:
<?xml version="1.0" encoding="utf-8"?>
<Object>
<Contents>
<Source="address" Title="title"></Source>
<Description><h1>New hot Features</h1><p>The all new Piecemaker comes with lots of new features, making it even more slick.</p><p>Just to mention a few - you can now specify unlimited transition styles, include your own SWF and Video files, add hyperlinks to images and info texts with all special characters.</p><p>We also impoved the navigation and the animation with animated shadows and pixel-perfect transitions.</p></Description>
<? create php loop here ?>
</Contents>
</Object>
Also note
this line
<Source="address" Title="title"></Source>
might be wrong (you assigned some value to the tagname), try
<Source name="address" Title="title"></Source>
or something like that.
As I see generating xml file with php could be done in this way - for example you'll create file datasource.xml which will be not a static xml file but xml with php code included with contents like
<?php //php code to generate any xml code as Text
// it can be whatever you need to generate
// for example
$content="<h1>New hot Features</h1><p>The all new Piecemaker comes with lots of new features, making it even more slick.</p><p>Just to mention a few - you can now specify unlimited transition styles, include your own SWF and Video files, add hyperlinks to images and info texts with all special characters.</p><p>We also impoved the navigation and the animation with animated shadows and pixel-perfect transitions.</p>";
$output="<Description>".$content."</Description>";
header('Content-type: application/xml');// this is most important php command which says that all output text is XML it must be called before any line of xml will be printed.
// So you need at first generate XML as text then call this command and echo contents of your xml file.
?>
<?xml version="1.0" encoding="utf-8"?>
<Object>
<Contents>
<Source name="address" Title="title"></Source>
<? echo $output; ?>
</Contents>
</Object>
In order to allow php to execute php code inside XML file we need to add some directives to apache host configuration file. In my case I added
<IfModule mod_php5.c>
<FilesMatch "\.xml$">
SetHandler application/x-httpd-php
</FilesMatch>
inside my virtual host configuration file, or you can place this command inside .htaccess file in your directory if Override of this param is allowed in your host configuration.
And about xml- to make sure it's ok you can use http://validator.w3.org/ or http://www.w3schools.com/xml/xml_validator.asp to validate xml generated by your script.
Does this XML need to be a stored file somewhere on the server, or can you just pass it a string formatted like the XML you mentioned. You could write a function that generates the XML you're looking for and returns it, based on input and then call that like
function generateXML($input){
$xml = '<?xml version="1.0" encoding="utf-8"?><Object><Contents>
<Source="address" Title="title"></Source><Whateverelse>' . $input;
$xml .= '</Whateverelse></Contents></Object>';
return $xml;}
flashvars.xmlSource = generateXML("This is whatever else");
If you need to actually generate and store a well formed XML document, or if your XML is fairly complex and you need to generate an object rather than just using a string, you can utilize one of the PHP libraries to do this like http://php.net/manual/en/book.simplexml.php

Displaying an XML file with XSL within an existing webpage

I'm trying to get an html page to display an XML file formatted with an XSL stylesheet. Whatever examples I see are either displaying it in a new page, with the XSL stylesheet taking care of the tags, but no examples where I can clearly see it being displayed as part of an existing webpage...
I'm using a PHP script to generate the HTML. And the XML data is being generated by another PHP function (not under my control). The XSL file is uploaded on the server and stored at: /xsl/1234567890.xsl
Here's what the php outputs:
<html>
...
<body>
...
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="/xsl/1234567890.xsl"?>
...
<xml tags>
...
What am I doing wrong?
Two ways to transform the XML:
1 browser
Most browsers implement XSLT processors. You could use:
<iframe src="xml-source.xml"/>
The users will have to make three requests (page, xml, xsl) and unless you want inline scrollbars you'll need some Javascript to resize the iframe.
2 server
You can run a XSLT processor on the server side and return the transformed XML. There are many ways to do this, here is one in PHP. With caching you shouldn't run into any performance problems and also support browsers without internal XSLT processors (e.g. mobile devices).

What php function can display formated XHTML from a XML file?

Ive been trying to display formatted content blocks from a xml file with no luck. Ive been using simplexml_load_file and other varients which Im now seeing cannot deal with the xhtml tags within the called tag ... eg.
//php contents
<?php $xml=simplexml_load_file("file.xml");
echo ($xml->entry); ?>
//xml contents
<entry>
<p>This does not work</p>
</entry>
whereas
<entry>This works</entry>
can someone please tell me which php function can do this from an xml file and or
what is the best way to display the contents with xhtml formatting?
Im trying to dynamically load content to a webpage without having to build too many pages. I like the idea of having all my content in one xml file for easy edits.
Theres not enough content to justify a database yet.
Thanks in advance
You can try dumping the contents of a certain simplexml node (in this case: $xml->entry) using the asXml function.
echo $xml->entry->asXml();
Check the php documentation on simplexml here (link to the asXml() call):
Simplexml documentation
im getting closer... I found asXML() which outputs the html tags... but not sure yet how to point to specific blocks.... eg $xml->asXML(block) displays 1
got it
$xml->block->asXML()
works
would still like to know if there is a better method tho.

Categories