Quantcast
Channel: Obtain a Blogger's blog ID from its friendly URL without screen scraping - Stack Overflow
Viewing all articles
Browse latest Browse all 5

Answer by Jamal Fanaian for Obtain a Blogger's blog ID from its friendly URL without screen scraping

$
0
0

(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 autoloaderrequire_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';}

Viewing all articles
Browse latest Browse all 5

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>