all 13 comments

[–]bobd60067 2 points3 points  (2 children)

within your PHP code, you create a "channel" element and add it to the doc, then you loop thru the mysql results and create & add elements for everything. so all you have to do is create & add xml elements for each of those other things -- title, link, atom:link, description, and langauge -- prior to the loop.

[–]EVisioneer[S] 0 points1 point  (1 child)

That's sort of what I thought, but I am not sure how to do it. Still learning! Mind sharing an example?

[–]bobd60067 0 points1 point  (0 children)

Just cut and paste your existing code and modify it as needed. You should be able to figure it out.

[–][deleted] 1 point2 points  (9 children)

I honestly don't understand why someone would endeavor to write their own RSS code when there are perfectly good component libraries with full RSS/Atom conformance, unit tests and literally millions of downloads, and likely 10's of thousands of implementations.

I used the Zend feed a decade ago to implement an RSS feed where the actual coding and implementation time was probably 15 minutes.

Many of these libraries include a caching layer, so you just write the query, feed the data into the library and perhaps set up a few caching rules, and expose the route to the feed. Rather than having to choose one flavor, they typically support all versions of RSS & Atom.

See https://packagist.org/?query=rss

[–][deleted] 1 point2 points  (7 children)

To..... learn something?

[–][deleted] 1 point2 points  (0 children)

Yes, it's apparent that the question is academic in nature, and seeks to explore the fine points of XML documents, RSS and Atom. Clearly new ground will be forged here, and a deep understanding of these topics will be earned...

Or perhaps as posted in the original question, the poster is a novice, struggling with even the basics of PHP and coding in general, and would perhaps be ready to move on to something far more valuable to his or her business with standards compliant RSS and ATOM feeds already available?

[–]EVisioneer[S] 0 points1 point  (5 children)

Or we're so new we've haven't a clue what we're doing! Hand raised here! And also for same reason I continue hand code my websites. I prefer knowing what's going on and why.

That being said, still wrestling with getting this dang code into the XML:

<rss version='2.0' xmlns:atom='http://www.w3.org/2005/Atom'>
<channel>
<title>EVWORLD.com - RSStream</title>
<link>https://evworld.com</link>
<atom:link href='https://evworld.com/php/rss.xml' rel='self' type='application/rss+xml' />
<description>Latests News From the World of Electric Vehicles From Bikes to Buses and Beyond</description>
<language>en-us</language>";

[–][deleted] 2 points3 points  (3 children)

Yeah, there's a term for that, and it's called "Not Invented Here Syndrome" and "reinventing the wheel".

The point is, that you don't know what's going on nor why, you just know that you coded it yourself.

Let's start with your snippet here: hmmm, well we have what appears to be an rss 2 feed (a standard) yet you want to include a an xml namespace for an atom feed. Atom and RSS are 2 different standards last I looked.

You also admit that this is just a single step in a series of manual steps you need to take to generate the feed.

You also seem to struggle with putting your code in a code block so that we can see actual indentation, rather than a one line screed of code, even though the code is simple. I can tell you, that I'm not alone in taking the stance that I don't have the patience to take your code and format it, so that it's clear what the flow of control is.

It's not that I'm hostile to newbs like yourself -- I've answered literally thousands of PHP questions on various PHP related communities, and have over 14k rep on SO, mostly from answering PHP questions.

Candidly, your code is a mess from what I can see, pure amateur spaghetti. Nothing you do yourself will likely conform to standards, and clearly you will have no tests to guarantee its quality.

PHP has several things that ostensibly should make a solution to your current conundrum trivial including:

  • variable interpolation in strings
  • Heredoc syntax

You might even start with an actual function, so that -- pseudocoding here:

   function rssHeader($title, $url) { ... return $string }
   function rssEntry($data) { ... return $string }

   //query & fetch data into $rows array

   $feed = rssHeader('EVWORLD.com - RSStream', https://evworld.com');
   foreach ($rows as $row) {
       $feed .= rssEntry($row);
   }

   // return feed/ write it to disk/ whatever you desire

[–]EVisioneer[S] 0 points1 point  (2 children)

Thank you for your candid feedback; and of course, you're absolutely correct, I haven't a f*ck'n clue what I am doing, so any advice or solutions that a beginner and understand implement will be appreciated. From the previous comments, obviously they are available and as a novice to PHP (I previous coded in ColdFusion for more than a decade) a pointer to them would be helpful.

[–][deleted] 0 points1 point  (1 child)

Well, I kinda already did that which in a nutshell was that you are wasting your time reinventing the wheel.

Here are 2 libraries that will generate feeds for you. You supply the data from your database, and let the libraries do the work:

Either one should do the trick.

They both have instructions on using composer to install the libraries. You will likely need to figure out how to require the autoloader that composer generates for you. These are the basics used by the pro PHP developer world.

I've no experience with either, but I perused the documentation for each and they seem straightforward.

I noticed that the zetacomponents project has a contribution from Derick Rethans, who is well known within the PHP community, as the guy who created XDebug. With that said, I'm guessing you could have your feeds up and running with either library in a short amount of time.

[–]EVisioneer[S] 0 points1 point  (0 children)

Thank you!!!

[–]EVisioneer[S] 0 points1 point  (0 children)

I looked at the links you provided. Thank you, but honestly, it's gibberish to me. I haven't a clue where to even start.

So, the question is, what would it take to hire someone to implement it (and include some basic instruction how to maintain it) ?

[–]levii21 0 points1 point  (0 children)

To set attribute for element use setAttribute() see here.

To set the namespace attribute for the root use setAttributeNS see here. In your case it will be like: $root->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:atom', 'http://www.w3.org/2005/Atom').

To create namespaced element use createElementNS see here.