Obtain a Blogger's blog ID from its friendly URL without screen scraping
I'm using the Blogger API for PHP. I want to obtain the blog ID from just the site's name.
For example, http://sleeptalkinman.blogspot.com/
has a blog ID of 3117168333067506122. This is possible as the blog ID appears in the source, but screen scraping isn't a good idea!
Is it possible to obtain it through the API 开发者_开发技巧instead of scraping it from the HTML?
Today, the newest blogger API is in version 3.0. With that new API, we can get blog ID with resource type getByUrl
. This is full example using my blog and my API:
https://www.googleapis.com/blogger/v3/blogs/byurl?url=http%3A%2F%2Fwww.ifaniqbal.com&key=AIzaSyDNkR52eSfObZi9BPKrTytbowOAM7Js9uY
Just Ctrl+C that example and Ctrl+V to your browser to test that. One of return field of that example is 'id' field, which is the blog ID that we want (in JSON), like this:
{
"kind": "blogger#blog",
"id": "7107469463017369923",
"name": "Ifan Iqbal",
"description": "Tips, Download, Resensi, Tutorial, Blog, Blogspot",
"published": "2012-02-28T23:54:48+07:00",
"updated": "2013-07-11T16:24:58+07:00",
"url": "http://www.ifaniqbal.com/",
"selfLink": "https://www.googleapis.com/blogger/v3/blogs/7107469463017369923",
"posts": {
"totalItems": 161,
"selfLink": "https://www.googleapis.com/blogger/v3/blogs/7107469463017369923/posts"
},
"pages": {
"totalItems": 2,
"selfLink": "https://www.googleapis.com/blogger/v3/blogs/7107469463017369923/pages"
},
"locale": {
"language": "in",
"country": "",
"variant": ""
}
}
(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';
}
$blog = "sleeptalkinman.blogger.com";
$username = split('\.', $blog)[0];
should give you what you're after, as long as you have the URL like above.
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
精彩评论