How To Get Your Blog’s Posts

Getting your blog’s posts and displaying them somewhere else is easy. You’ll just have to know how to read an XML file first.

Your Blog’s ID

Each blog has its own ID from which we can get the posts feed. To get your blog’s id simply open up your dashboard and click “New Post”, your blog id will then be visible in the URL.


Blog ID in URL

How to Get The Posts With PHP

The URL for a blog’s feed has the following format.
http://www.blogger.com/feeds/blogID/posts/default
If you take this URL, put your blog’s id and paste navigate to it in your browser, you will see that you get an XML response with your posts.
This XML response has an <entry> tag for each one the posts. Inside the entry tag there are other tags such as <title>,<content>, <published> and more. These are the tags we will refer to in our PHP script.

How To Read The Response

Here is a simple PHP script you can use to display your posts’ title, content and published date. If you want to learn about reading XML files in detail check out my post on this subject.
<?php
$blogID='xxxxxxxxxxx';
$requestURL="http://www.blogger.com/feeds/{$blogID}/posts/default";
$xml=simplexml_load_file($requestURL);
?>
<html>
<body>
<?php
foreach($xml->entry as $post)
{
echo '<div>';
echo '<h2>'.$post->title.'</h2>'; // post title
echo '<p>Published: '.$post->published.'</p>'; // date published
echo '<p>'.$post->content.'</p>'; // post content
echo '</div>';
}
?>
</body>

Post a Comment