suburban glory

A Blog in Suburban Glory:
Web Dev Ideas and Inspiration

Using PHP to validate an RSS feed URL.

|| Category: PHP ||

I'm just working on a script at the moment that allows users to submit a blog URL and the blog RSS feed.

To check whether the submitted URL is a properly formatted address I use FILTER_VALIDATE_URL but there is no such filter function for RSS feeds.

So after putting on my thinking hat I came across the following way of checking whether a URL is an RSS feed.

Firstly, I used cURL to take the entire contents of the page and save it in a txt file:

$ch = curl_init(trim($_POST['blog-rss']));
$fp = fopen(LIB  . DS ."textfile.txt", "w");

curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);

curl_exec($ch);
curl_close($ch);
fclose($fp); 

Then I took the contents of the text file and put it into a string:

$filename = LIB  . DS ."textfile.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));

From there I used the stristr() function.

if (stristr($contents, "version="1.0"")) {
	null;
} else {
	$error[] = "There appears to be a problem with the submitted RSS field";
}
RSS Logo

But this is where the problem lies. There are several different feed formats and the only constant I could find between them was the opening XML declaration: “version=1.0”.

So the URL is passed as okay if “version=1.0” is found within the contents of the file. However, this is also in a XML sitemap so while it passes RSS and Atom feeds, it will also accept other different types of files.

Do you have a better solution for this?

Comments

You can add comments below.
Auto-paragraphing is enabled. Double newlines will be converted to paragraphs.

Allowed tags: a, abbr, acronym, b, blockquote, caption, cite, code, dd, del, dfn, div, dl, dt, em, i, ins, kbd, li, ol, p, pre, s, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, var.

If you wish add code examples then please do so between the following tags:

<pre><![CDATA[
Place code here
]]></pre>







mainland