tags:

views:

521

answers:

4

Hi,

Im using the Blogger API: http://code.google.com/apis/blogger/docs/1.0/developers_guide_php.html

What I wish to do is obtain the blog ID from just the sites name eg, sleeptalkinman.blogger.com

This is possible as the blog ID appears in the source but screen scraping isnt a good idea!

Any advice appreciated! Im using PHP.

For clafification I require the blog ID, which again for the example of sleepwalkingman is 3117168333067506122. Id like to know if you could obtain it through the API instead of scraping it from the html.

A: 
$blog = "sleeptalkinman.blogger.com";

$username = split('\.', $blog)[0];

should give you what you're after, as long as you have the URL like above.

KingRadical
Well, then you'd have to echo `$username[0]`, as `$username` will be an array...
ceejayoz
You're right, my [0] went missing somehow. Edited to fix.
KingRadical
+1  A: 

(I had posted an original answer, but modified it to use Zend_Gdata instead).

Here is a method of getting the blog ID.

<?php

$user = 'username';
$pass = 'password';

// I have to admit, I would normally use the autoloader
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_Query');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Feed');

$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, 'blogger', null,
    Zend_Gdata_ClientLogin::DEFAULT_SOURCE, null, null,
    Zend_Gdata_ClientLogin::CLIENTLOGIN_URI, 'GOOGLE');
$gdClient = new Zend_Gdata($client);

/**
 * Get the blog ID
 * @param string $feed URL to blog feed or blog name
 *  Example:
 *   - http://googleblog.blogspot.com/feeds/posts/default
 */
function getBlogId($gdClient, $feed)
{
    // You could build the /feed/posts/default part yourself and just pass
    // googleblog.blogspot.com:
    // $feed = 'http://' . $feed . '/feeds/posts/default';
    $query = new Zend_Gdata_Query($feed);
    $feed = $gdClient->getFeed($query);
    preg_match('/blog-([0-9]+)/', $feed->id->text, $match);

    if (isset($match[1]))
    {
        return $match[1];
    }

    return false;
}

echo getBlogId($gdClient, 'http://sleeptalkinman.blogspot.com/feeds/posts/default');

Original answer

If you're trying to retrieve information, then you should simply be able to replace the www.blogger.com part and ignore the blogID. For example, if you're trying to find all posts from http://dailyvim.blogspot.com/ you would use:

http://dailyvim.blogspot.com/feeds/posts/default

Instead of the normal URL, http://www.blogger.com/feeds/[blogID]/posts/default

This method may also work for publishing to the blog, so long as the authenticated user has write access to it. I have not been able to test this, though.

Getting the blog ID

You can get the blog ID from the feed above using the following:

$content = file_get_contents('http://sleeptalkinman.blogspot.com/feeds/posts/default');
preg_match('/<id>.*blog-([0-9]+)</id>/U', $content, $match);
print $match[1]; // Prints the blog ID

Getting post IDs for latest posts

You can also get the latest posts from the above feed (this time I'll use SimpleXML instead):

$feed = simplexml_load_file('http://sleeptalkinman.blogspot.com/feeds/posts/default');
foreach ($feed->entry as $entry) 
{
    // I'm getting both the blog ID and post ID
    preg_match('/blog-([0-9]+).*post-([0-9]+)/', $entry->id, $match);
    print $match[2];

    // Now you can use the following URL with the blogger API
    $comment_feed_url = 'http://www.blogger.com/feeds/' . $match[1] . '/' . $match[2] . '/comments/default';
}
Jamal Fanaian
Thanks but what Im asking is a step to a bigger question where I wish to retreive the comment feed for a specific post. Again, example!http://www.blogger.com/feeds/3117168333067506122/5498538184176328230/comments/defaultSo really Id like to retreive the blog ID fro the first parameter then post ID for the second.
joobaal
Well, you can easily get the blog ID from the feed. I've edited my original answer with code to do that.
Jamal Fanaian
Yeah I can do that but I want to avoid loading the page, I was just looking for Blogger API functionallity to return it.
joobaal
I've posted a method of getting the blog ID using Zend_Gdata. Please note that Zend_Gdata *is* making an HTTP call to the feed regardless. This method will better integrate with the API though.
Jamal Fanaian
A: 

Can someone tell me how to obtain the theumbnail from a Blogger post using PHP/Zend?

Joe Privett
If you want to ask a question, don't put it in an answerbut start a new thread for it.The "Ask Question" button is in the top right of this page.More people would see your question and try to answer that way.
sth
A: 

Seems you can use the blog's url to get the comments as well, try;

http://someusername.blogspot.com/feeds/comments/default

And you can get all the comments for a given post in the similar way, namely /feeds/xxxxxxxx/comments/default, where xxxxxxxx is the post ID.

And you can qualify by date by using the query string ?published-min=2010-03-16T00:00:00&published-max=2010-03-24T23:59:59

As a side note, the Blogger API can return the feed as JSON rather than XML, which I found way easier to deal with than parsing XML by using the query string ?alt=json

Mike P
Oh, and the JSON format is described at http://code.google.com/apis/gdata/docs/json.html
Mike P