我正在使用Blogger API for PHP。我想从网站名称中获取博客ID。
例如,http://sleeptalkinman.blogspot.com/
的博客ID为3117168333067506122.这可能是因为博客ID出现在源代码中,但屏幕抓取不是一个好主意!
是否可以通过API获取它而不是从HTML中抓取它?
答案 0 :(得分:3)
今天,最新的博客API版本为3.0。使用该新API,我们可以获得资源类型为getByUrl
的博客ID。这是使用我的博客和我的API的完整示例:
https://www.googleapis.com/blogger/v3/blogs/byurl?url=http%3A%2F%2Fwww.ifaniqbal.com&key=AIzaSyDNkR52eSfObZi9BPKrTytbowOAM7Js9uY
只需按Ctrl + C示例,然后按Ctrl + V到您的浏览器进行测试。该示例的返回字段之一是“id”字段,它是我们想要的博客ID(在JSON中),如下所示:
{
"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": ""
}
}
答案 1 :(得分:2)
(我发布了一个原始答案,但修改后使用Zend_Gdata代替)。
以下是获取博客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');
原始答案
如果您正在尝试检索信息,那么您应该只需更换www.blogger.com部分并忽略blogID。例如,如果您要查找来自http://dailyvim.blogspot.com/的所有帖子,请使用:
http://dailyvim.blogspot.com/feeds/posts/default
取代普通网址,http://www.blogger.com/feeds/[blogID]/posts/default
只要经过身份验证的用户具有写入权限,此方法也可用于发布到博客。但是,我无法对此进行测试。
获取博客ID
您可以使用以下内容从上面的Feed中获取博客ID:
$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
获取最新帖子的帖子ID
您还可以从上面的Feed中获取最新帖子(这次我将使用SimpleXML):
$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';
}
答案 2 :(得分:-1)
$blog = "sleeptalkinman.blogger.com";
$username = split('\.', $blog)[0];
只要您拥有上述网址,就应该为您提供所需的内容。
答案 3 :(得分:-1)
似乎你可以使用博客的网址来获取评论,试试;
http://someusername.blogspot.com/feeds/comments/default
你可以用类似的方式得到给定帖子的所有评论,即/ feeds / xxxxxxxx / comments / default,其中xxxxxxxx是帖子ID。
您可以使用查询字符串按日期限定资格吗?published-min = 2010-03-16T00:00:00&amp; published-max = 2010-03-24T23:59:59
作为旁注,Blogger API可以将Feed作为JSON而不是XML返回,我发现使用查询字符串解析XML比使用XML更容易处理?alt = json