What file extension should I use for dynamic RSS - php

I am currently developing a dynamic RSS feed that will automatically pull articles from a MySQL database. The code is below
<?php
//Include the post retreival script
require_once '../phpScripts/rss_db_setup.php';
//Set the content type
header('Content-type: text/xml');
//Set up the RSS feed information
echo '<?xml version="1.0" encoding="ISO-8859-1"?>'.
'<rss version="2.0">'.
'<channel>'.
'<title>Company Name</title>'.
'<link>http://www.company.ca</link>'.
'<description></description>'.
'<category></category>';
//Retreive posts from the database
$rssData = new rssData();
echo $rssData->generateFeed($dbcon);
//Close the feed
echo '</channel></rss>';
?>
I am wondering whether this file should be saved as a .xml or a .php? I have added the following line to my .htaccess file, but do not really understand exactly how it works
AddType application/x-httpd-php .xml
Is this a correct way to do this? Or should I use another htaccess function such as modRewrite, or use a CRON job to generate a new .xml every day or so?

RSS doesn't need a extension. It doesn't care if the url is /feed.php /feed.xml or even just /feed/. It's not like the files on your own harddrive. HTTP send the content-type header to specify what kind of file it is.
It however (officially) needs the correct content-type header. You specify text/xml in your code, which should officially be application/rss+xml.
Using AddType could be a problem if you use static xml files elsewhere on the server. PHP would choke on the part at the beginning of every xml file, give a nice error message resulting in invalid xml.

With the AddType application/x-httpd-php .xml line, Apache will be able to serve XML files containing some PHP code inside. So you can save this file as a .xml, the PHP code will be interpreted
Perhaps you should add a cache manager in order to avoid generating the same feed when there is no new article?

Related

using PHP to remove the extension from a file and then downloading it

I recently had a asked a question very similar to this one, however after evaluating that I did not explain it in the best way I have come back once again explaining it in a greater manner.
So, I am creating a system that will gather data from a MySQL database and use a unique id to download a file, however depending on the value of a column within that database called type, this file could be anything from a png file to an xml file. What I am currently doing is trying to download these files WITHOUT any extension.
As an example to maybe make this easier to understand, a file named image.png would be converted to just image and then downloaded.
With this you could rename the file to image.png again on the local machine and view the image.
This may seem very inefficient to most reading this but for my current situation it's all that will work.
How could I remove a files extension and then download it? (in php)
Thank you in advance.
Just use headers to specify response type.
$filepath = '/wherever/the/file/is.png';
$filename = 'new-cool-name';
header('Content-Type: whatever/content-type-is');
header("Content-disposition: attachment;filename=$filename");
readfile($filepath);
This basically sends a response with specified content-type as an attachment and the body of the attachment contains the file contents. If you never sure what's the content type is, then just use application/octet-stream
Usually when you set out to push a file for downloading from a serverside script, you do so by utilizing http headers like https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition
The filename of the downloadable file is specified in that header
Okay so to remove an extention from a file you could do is
$withoutExtion = preg_replace('/\\.[^.\\s]{3,4}$/', '', $youfilename);
...followed by your file download code

PHP Parse a PHP file echoing XML data

I have a php file which generates and echo's XML data.
So basically it shows a XML but it's a PHP file.
I need to read and parse this data.
I've seen this: simplexml_load_file('some.xml'); but in this case I cannot do this as I've got the xml as a php file.
How can I do this?
Just the same way, the file extension doesn't matters (.xml or .php) what it really matters is the actual contents of the file, so if your file have a .php extension but its contents are valid xml then you should have no problem:
simplexml_load_file('somepage.php'); //this is fine
Set mime type to xml header('Content-Type:text/xml');

Does an RSS feed have to be an XML file?

I ask this because I see only two xml files in a wordpress blog, wlwmanifest.xml and default.xml and neither look like an rss feed. However I do see a php file called feed-rss2.php that looks like an rss feed. Everything I've ever read says that rss feeds have to be xml files. Am I wrong? Can they be php files with xml code inside?
They are just outputting XML code with XML headers. The actual file doesn't have to be an XML file, just the response has to be text/xml and contain XML output. You can do the same for things like CSS files... anything really.
There is no such thing as a file extension in HTTP.
A client requests a URI from a server. The server responds with a Content-Type HTTP header that says what type of file it is sending back, and the file in the HTTP body.
The client doesn't care (and can't know) if the server generated that response by reading a static file, by running a program, or by some other means.
There is no difference to the client between a PHP program that outputs XML and a static XML file.
The RSS Readers would be looking only on the client side, not the server side. In the wordpress installations, the feed-rss2.php is a PHP File, processed by the server, by giving the correct headers in the format:
header("Content-type: text/xml");
So that the readers get to know that it is a XML file and not a PHP file. As Robbo said, the actual file doesn't have to be an XML file, just the response has to be text/xml and contain XML output.
Even the same case with the styles. If you see the wordpress's style.php, it would have something like:
header("Content-type: text/css");
include($theme . "/style.css");
So that, it uses PHP's power to read the appropriate file and display the output in the same URL. Easy isn't it?

PHP Print Included XML File

I'm trying to echo out an XML File via a PHP File. Like so:
Url visits viewxml.php?id=1
Php open a specific XML file, such as "xmlfile_".$_GET['id'].".xml" and prints it as if they are visiting the xml file itself.
The reason I'm using PHP is to do Session checks to make sure they are allowed to view the xml file.
This is how I am doing this:
header("Content-type: text/xml; charset=utf-8");
// send the saved XML file.
include "xmlfile_".$id.".xml";
Doing this gives me the following GC XML error:
And there is nothing below it.
I have tried different approaches, such as printing it or turning it into a string but then I have problems reading the XML on certain software. I am hoping one of you can help me.
Thanks!
include() will attempt to evaluate the file; if there's something in there that looks like PHP then it will corrupt your results.
Try using readfile instead; this will only output the file contents.

I need to provide a XML file so my Android can fetch it

I really am trying to code from PHP to have a XML file. I got the code that can display items from PHPmyadmin. but how can I display those things in XML file is really my problem, I tried all the codes. I could find. But, still no luck.
While generating xml your first line in php file should be
header ("content-type: text/xml; charset=utf-8");
then use echo statement & print whatever you want within xml tags like
echo "<status>0</status>";
just follow xml standerds & test using IE will be better.

Categories