Does an RSS feed have to be an XML file? - php

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?

Related

PHP: save a file (generated by aspx) to server

I have an url provided by a wholesaler. Url generates xml file which I need to save on my server.
I use PHP - file_get_contents and file_put_contents to do that:
$savepath = "path_to_my_server_folder";
$xmlurl = "http://usistema.eurodigital.lt/newxml/xmlfile.aspx?xml=labas&code=052048048048051057049050048049052";
file_put_contents($savepath.'eurodigital.xml', file_get_contents($xmlurl));
File is generated on my server, but its content is empty. I have no problems with other xml files if I provide direct xml url, but in this situation file is generated by aspx file dynamically. Xml url is actual url I use. When I open xmlurl in browser, xml file gets saved to device.
Can you help me with moving xml to my server? What function should I use? Is it even possible to do that using PHP 5? "allow_url_fopen" is ON.
Thank you in advance!

How can I tell the filetype of images without extension? How do they work?

I am dealing with images on the web that come without a file extension, like this:
https://static1.squarespace.com/static/52a74d9ae4b0253945d2aee9/t/52ed63b1e4b04368c021b921/1463088116168/?format=500w
Images like these can be found, e.g., on websites made with squarespace, like this demo: https://bedford-demo.squarespace.com/
I'm trying to download these images and store them on my server, using PHP. But how can I find out the actual URL of those images? How does this work? And how can I tell the filetype of this image? What is this sorcery?
Any hints are appreciated!
Quick answer:
To find out the Content-Type returned for any URL, look at this answer:
Get Content-Type of requested URL in PHP
Why you need the Content-Type:
Just like how not every webpage on the internet has an URL that ends with .html, images are not required to have an "extension" in their URL either.
What determines whether the browser will treat the data retrieved from the URL as an image is the Content-Type header in the HTTP response.
The URL you posted returns the following HTTP headers:
For HTML documents the Content-Type is text/html. You can inspect the headers as you browse by opening the Network tab of the developer console in your browser. Look for the "response headers".
You can get the mime type of the file with getimagesize:
<?php
$size = getimagesize("https://static1.squarespace.com/static/52a74d9ae4b0253945d2aee9".
"/t/52ed63b1e4b04368c021b921/1463088116168/?format=500w");
print_r($size["mime"]);
?>
Prints:
image/jpeg

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');

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.

What file extension should I use for dynamic RSS

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?

Categories