PHP RSS not posting to Social Media Sites

240 views Asked by At

I created my own feed for my website and connected it to feedburner and to dlvr.it for dissemination to social media sites.

However, whenever I create a new post in my site, it takes a few minutes to post to feedburner (which is not really a problem) and it does not post updates to my social media accounts and upon viewing dlvr.it's reports, it says 'no items found via pull'

My complete code for my xml which is connected to feedburner goes like this:

 <?php
include('db.php');

header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="ISO-8859-1"?>  
<rss version="2.0">  
<channel>  
<title>MindWeather Thesis</title>  
<description>Latest News from my website</description>  
<link>http://www.MySite.info</link>';  

$get_articles = "SELECT ID, NewsType, Content,   
DATE_FORMAT(DateTime,'%a, %e %b %Y %T') as formatted_date   
FROM tblnews ORDER BY DateTime DESC LIMIT 15";  

$articles = mysql_query($get_articles) or die(mysql_error());  

while ($article = mysql_fetch_array($articles)){  

    echo '  
       <item>  
          <title>'.$article['NewsType'].'</title>  
          <description><![CDATA['.$article['Content'].']]></description>  
          <link>http://www.MySite.info</link>  
          <pubDate>'.$article['formatted_date'].' GMT</pubDate>  
      </item>';  
} 
echo '</channel>
</rss>';
?>

What's wrong?

1

There are 1 answers

3
Matt Gibson On

First of all, move the header() call up before your first echo(). You should always output headers before any other output, because they put information into the HTTP header, which should always be complete before the actual body starts appearing. As it says in the manual:

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

Second, let's have a look at the output you're generating. You've not provided the output from your PHP (FeedBurner changes things on the way through to try to help make feeds more valid/useful), but this is roughly what you're generating, assuming a single item in the database:

<?xml version='1.0' encoding='ISO-8859-1' ?>
<rss version='2.0'>
Mindweather
  <channel>http://www.MySite.info
    <title>MyProj</title>
    <item>
      <title>NewsType</title>
      <link>http://www.MySite.info/signin.ph</link>
      <description>Content</description>
    </item>
    <link>MyProj/link>
  </channel>
</rss> 

So, there's a few problems there. You can compare your output with the RSS spec, or run it through the W3 Feed Validator Service to get some tips, or even compare your feed to a basic example, like the one on Wikipedia, but basically:

  • The word "Mindweather" appears in an invalid place. Remove it, or move it where it needs to be. Is it the title of the feed? If so, it should be where "MyProj" is, in the <title> of the <channel>.

  • "http://www.MySite.info" appears in an invalid place. If that's meant to be a link to your site, it should be in the <link> element of the <channel>, near the end, where you have echo "<link>MyProj/link>

  • (While I'm there, you're missing the open angle bracket from the </link> on that line, too.)

  • Your <channel>'s <link> element should be before all your items.

  • Your <channel> needs a <description> element.

On top of that, you don't have a pubDate or a guid element in your feed.

Even though pubDate and guid are technically optional, things that regularly pull content from RSS feeds tend to look for one or both of them to see if they've seen them before or not. The pubDate should be the publication date of the new item, and the guid should be a unique identifier for it (often the URL of the specific item is used.) See the RSS 2.0 spec for more details.

For example, often something that's pulling from a feed will only pick up items from the last 30 days, say, so it'll look for any items with a pubDate more recent than that. If none of your items have a pubDate, it won't "see" any of them.

When I'm developing a feed, I'll generally grab an example as a template and make my output look like that, only with my details in it.

Finally, bear in mind that the mysql extension is deprecated, and will be removed from future versions of PHP. You shouldn't be using it for new code.