14.5. Using Google Blogger

The weblog hosting service Blogger.com participates in the Google Data API. You can write PHP applications using Zend_Gdata_Blogger to query existing blogs, and post new entries to blogs for which you have authenticated access.

See http://code.google.com/apis/blogger/overview.html for more information about the Google Blogger API.

14.5.1. Understanding Blogger version compatibility

There are some differences regarding using the Google Data API to access blogs that are hosted on the current version of Blogger, versus blogs that are hosted on the version of Blogger that is currently in Beta. The most significant differences are related to authentication. See the table below:

表 14.1. Differences between current Blogger service and Blogger beta service

Characteristic Current Blogger service Beta Blogger service
Supports Google Data API Yes Yes
Credentials based on Blogger.com account Google.com account
Supports ClientLogin authentication Yes Yes
Supports basic HTTP authentication Yes No
Supports AuthSub authentication No Yes
Format for unauthenticated feeds Atom 0.3 Atom 1.0
Format for authenticated feeds Atom 1.0 Atom 1.0
Format for posting entries Atom 1.0 Atom 1.0
Supports query parameters No Yes

14.5.2. Querying a blog

Use the setBlogName() method to specify the name of a blog. This is the name that appears in a blogspot URL, for example: http://blogname.blogspot.com. You can also specify the name of the blog when you request a feed.

<?php
$gdataBlog = new Zend_Gdata_Blogger();

// One way to get a feed for a blog...
$feed = $gdataBlog->getBloggerFeed('blogname');

// Another way to get a feed for a blog...
$gdataBlog->setBlogName('blogname');
$feed = $gdataBlog->getBloggerFeed();
?>

See the documentation for Zend_Feed for more information on how to use this object.

The setPublishedMin() and setPublishedMax() methods allow you to specify bounds on the entry date. If you specify a value for publishedMin, no entries that were published earlier than the date you specify are included in the feed. Likewise no entries published after the date specified by publishedMax are included.

The entries in the feed are ordered by the <updated> element, with the most recent entry appearing first in the feed.

[注意] No support for category or full-text queries

The Google Blogger API currently does not support queries using categories, keywords, or tags. If you try to specify a search using setCategory(), Zend_Gdata_Blogger throws an exception.

The Google Blogger API currently does not support queries using full-text search terms. If you try to specify a search using setQuery(), Zend_Gdata_Blogger throws an exception.

14.5.3. Posting to your blog

You can use the post() method to post entries to your blog. To post entries, you must be using an authenticated Zend_Http_Client that you created using the Zend_Gdata_AuthSub or Zend_Gdata_ClientLogin classes.

<?php
$gdataBlog = new Zend_Gdata_Blogger($authenticatedHttpClient);
$gdataBlog->setBlogName('myblog');

$xmlString = <<<XML
<entry xmlns='http://www.w3.org/2005/Atom'>
  <title type='text'>Marriage!</title>
  <content type='xhtml'>
    <div xmlns="http://www.w3.org/1999/xhtml">
      <p>Mr. Darcy has <em>proposed marriage</em> to me!</p>
      <p>He is the last man on earth I would ever desire to marry.</p>
      <p>Whatever shall I do?</p>
    </div>
  </content>
  <author>
    <name>Elizabeth Bennet</name>
    <email>liz@gmail.com</email>
  </author>
</entry>
XML;

$xml = new SimpleXMLElement($xmlString);

$response = $gdataBlog->post($xml->asXML());
?>

You can get a feed that names the blogs for your currently authenticated account using the getBloggerListFeed() method.

<?php
$gdataBlog = new Zend_Gdata_Blogger($authenticatedHttpClient);
$myBlogs = $gdataBlog->getBloggerListFeed();
?>