KC Kern
KC Kern

Reputation: 400

Listing Blogger Posts via API v3 with PHP Google_Client, Google_Service_Blogger

I am using the Google PHP client library for accessing Google APIs (See reference)

I am trying to get a listing of posts from a private blog (same content that would find in an RSS feed). Private blogger blogs obviously dont have open RSS feeds, so this is my attempt to both

  1. programmatically retrive the blog contents
  2. satisfy the privacy restrictions on the blog.

The token used in the API client is an authorized reader of the blog.

Here's the code. It all works great (connects, retrieves the correct blog object, etc) but fails when trying to get the post data itself using the getItems function (See library source, line 2007). An empty array is returned.

$client= new Google_Client();
$client->setClientId(GGL_CLIENTID);
$client->setClientSecret(GGL_SECRET);
$client->setRedirectUri(GGL_REDIRECT);
$client->refreshToken(GGL_TOKEN);
$service=new Google_Service_Blogger($client);

$blog      = $service->blogs->getByUrl('http://MYBLOG.blogspot.com/');
$blogName  = $blog->getName();
$blogUrl   = $blog->getURL();
$postsObj  = $blog->getPosts();
$postCount = $postsObj->getTotalItems();
$posts     = $postsObj->getItems();

echo "BLOG NAME: $blogName \n";
echo "BLOG URL: $blogUrl \n";
echo "TOTAL POSTS: $postCount \n";
echo "POST DATA: \n"; print_r($posts);

Given that the correct number of posts appears via getTotalItems, I am convinced all the plumbing is correct. What will it take to get the post data returned?

Note: I realize the client library is in beta, so this may be a hole yet to be filled.

Upvotes: 4

Views: 2720

Answers (3)

Aamer Shahzad
Aamer Shahzad

Reputation: 2947

$blogger = new Google_Service_Blogger( $this->client ); // pass authenticated client
$posts = $blogger->posts->listPosts($blogId); // blog id : e.g 1751515248423926432
echo "<pre>";
print_r( $posts );
echo "</pre>";

Upvotes: 0

user1299518
user1299518

Reputation:

Here's how you can list a specific number of posts (if you get limited) from a blog:

$id = $blog->getId();
$posts = $service->posts->listPosts($id, array("maxResults"=>5));
foreach($posts as $post) {
   echo "<br/>title:  ".$post->getTitle();
   echo "<br/>url:  ".$post->getUrl();
   echo "<br/>labels:  <pre>".print_r($post->getLabels(),TRUE)."</pre>";
   // etc.
}

Upvotes: 0

Trung Quan
Trung Quan

Reputation: 33

echo "POST DATA: \n"; print_r($posts);

your replace:

$blogId = $blog->getId();
$post = $service->posts->listPosts($blogId);
print_r($post);

Upvotes: 3

Related Questions