【问题标题】:Listing Blogger Posts via API v3 with PHP Google_Client, Google_Service_Blogger使用 PHP Google_Client、Google_Service_Blogger 通过 API v3 列出 Blogger 帖子
【发布时间】:2015-05-28 02:04:32
【问题描述】:

我正在使用 Google PHP 客户端库来访问 Google API(请参阅 reference

我正在尝试从私人博客中获取帖子列表(与 RSS 提要中的内容相同)。私人博主博客显然没有开放的 RSS 提要,所以这是我对两者的尝试

  1. 以编程方式检索博客内容
  2. 满足博客的隐私限制。

API 客户端中使用的令牌是博客的授权读者。

这是代码。这一切都很好(连接、检索正确的博客对象等),但在尝试使用 getItems 函数获取帖子数据本身时失败(参见library source,第 2007 行)。一个empty array is returned

$client= new Google_Client();
$client->setClientId(GGL_CLIENTID);
$client->setClientSecret(GGL_SECRET);
$client->setRedirectUri(GGL_REDIRECT);
$client->refreshToken(GGL_TOKEN);
$service=new Google_Service_Blogger($client);

$blog      = $service->blogs->getByUrl('http://MYBLOG.blogspot.com/');
$blogName  = $blog->getName();
$blogUrl   = $blog->getURL();
$postsObj  = $blog->getPosts();
$postCount = $postsObj->getTotalItems();
$posts     = $postsObj->getItems();

echo "BLOG NAME: $blogName \n";
echo "BLOG URL: $blogUrl \n";
echo "TOTAL POSTS: $postCount \n";
echo "POST DATA: \n"; print_r($posts);

鉴于通过 getTotalItems 显示的帖子数量正确,我相信所有管道都是正确的。将返回的帖子数据需要什么?

注意:我知道客户端库处于测试阶段,所以这可能是一个尚未填补的漏洞。

【问题讨论】:

    标签: php blogger google-api-php-client


    【解决方案1】:

    echo "POST DATA: \n"; print_r($posts);

    你的替换:

    $blogId = $blog->getId();
    $post = $service->posts->listPosts($blogId);
    print_r($post);
    

    【讨论】:

      【解决方案2】:

      您可以通过以下方式列出博客中特定数量的帖子(如果您受到限制):

      $id = $blog->getId();
      $posts = $service->posts->listPosts($id, array("maxResults"=>5));
      foreach($posts as $post) {
         echo "<br/>title:  ".$post->getTitle();
         echo "<br/>url:  ".$post->getUrl();
         echo "<br/>labels:  <pre>".print_r($post->getLabels(),TRUE)."</pre>";
         // etc.
      }
      

      【讨论】:

        【解决方案3】:
        $blogger = new Google_Service_Blogger( $this->client ); // pass authenticated client
        $posts = $blogger->posts->listPosts($blogId); // blog id : e.g 1751515248423926432
        echo "<pre>";
        print_r( $posts );
        echo "</pre>";
        

        【讨论】:

          猜你喜欢
          • 2014-06-25
          • 1970-01-01
          • 1970-01-01
          • 2011-08-05
          • 1970-01-01
          • 2017-07-25
          • 2018-02-15
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多