Skip to content

Instantly share code, notes, and snippets.

@vmartins
Created April 6, 2023 13:21
Show Gist options
  • Save vmartins/c2501f5de070fb9834dcc97fdc9398ec to your computer and use it in GitHub Desktop.
Save vmartins/c2501f5de070fb9834dcc97fdc9398ec to your computer and use it in GitHub Desktop.
Blogger API PHP
<?php
/**
* Composer Install:
*
* composer require google/apiclient:^2.12.1
*
* https://github.com/googleapis/google-api-php-client
* https://github.com/googleapis/google-api-php-client-services
*/
require "vendor/autoload.php";
$blogUrl = 'https://BLOG-NAME.blogspot.com';
$apiKey = 'YOUR-API-KEY'; // Create from: https://console.cloud.google.com/apis/credentials
$client = new Google\Client();
$client->setDeveloperKey($apiKey);
$blogger = new Google\Service\Blogger($client);
/**
* Methodos for 'blogger':
*
* $blogger->blogUserInfos->get($userId, $blogId, $optParams = [])
*
* $blogger->blogs->get($blogId, $optParams = [])
* $blogger->blogs->getByUrl($url, $optParams = [])
* $blogger->blogs->listByUser($userId, $optParams = [])
*
* $blogger->comments->approve($blogId, $postId, $commentId, $optParams = [])
* $blogger->comments->delete($blogId, $postId, $commentId, $optParams = [])
* $blogger->comments->get($blogId, $postId, $commentId, $optParams = [])
* $blogger->comments->listComments($blogId, $postId, $optParams = [])
* $blogger->comments->listByBlog($blogId, $optParams = [])
* $blogger->comments->markAsSpam($blogId, $postId, $commentId, $optParams = [])
* $blogger->comments->removeContent($blogId, $postId, $commentId, $optParams = [])
*
* $blogger->pageViews->get($blogId, $optParams = [])
*
* $blogger->pages->delete($blogId, $pageId, $optParams = [])
* $blogger->pages->get($blogId, $pageId, $optParams = [])
* $blogger->pages->insert($blogId, Page $postBody, $optParams = [])
* $blogger->pages->listPages($blogId, $optParams = [])
* $blogger->pages->patch($blogId, $pageId, Page $postBody, $optParams = [])
* $blogger->pages->publish($blogId, $pageId, $optParams = [])
* $blogger->pages->revert($blogId, $pageId, $optParams = [])
* $blogger->pages->update($blogId, $pageId, Page $postBody, $optParams = [])
*
* $blogger->postUserInfos->get($userId, $blogId, $postId, $optParams = [])
* $blogger->postUserInfos->listPostUserInfos($userId, $blogId, $optParams = [])
*
* $blogger->posts->delete($blogId, $postId, $optParams = [])
* $blogger->posts->get($blogId, $postId, $optParams = [])
* $blogger->posts->getByPath($blogId, $path, $optParams = [])
* $blogger->posts->insert($blogId, Post $postBody, $optParams = [])
* $blogger->posts->listPosts($blogId, $optParams = [])
* $blogger->posts->patch($blogId, $postId, Post $postBody, $optParams = [])
* $blogger->posts->publish($blogId, $postId, $optParams = [])
* $blogger->posts->revert($blogId, $postId, $optParams = [])
* $blogger->posts->search($blogId, $q, $optParams = [])
* $blogger->posts->update($blogId, $postId, Post $postBody, $optParams = [])
*
* $blogger->users->get($userId, $optParams = [])
*/
$blog = $blogger->blogs->getByUrl($blogUrl);
/**
* Methods for 'blog':
*
* $blog->getCustomMetaData()
* $blog->getDescription()
* $blog->getId()
* $blog->getKind()
* $blog->getLocale()
* $blog->getName()
* $blog->getPages()
* $blog->getPosts()
* $blog->getPublished()
* $blog->getSelfLink()
* $blog->getStatus()
* $blog->getUpdated()
* $blog->getUrl()
*/
$blogId = $blog->getId();
$posts = $blogger->posts->listPosts($blogId);
echo "Posts for {$blogUrl}: <br />\n";
foreach ($posts->getItems() as $post) {
/**
* Methods for 'post':
*
* $post->getAuthor()
* $post->getBlog()
* $post->getContent()
* $post->getCustomMetaData()
* $post->getEtag()
* $post->getId()
* $post->getImages()
* $post->getKind()
* $post->getLabels()
* $post->getLocation()
* $post->getPublished()
* $post->getReaderComments()
* $post->getReplies()
* $post->getSelfLink()
* $post->getStatus()
* $post->getTitle()
* $post->getTitleLink()
* $post->getTrashed()
* $post->getUpdated()
* $post->getUrl()
*/
echo " - {$post->getTitle()} <br />\n";
}
<?php
/**
* Composer Install:
*
* composer require google/apiclient:^2.12.1
*
* https://github.com/googleapis/google-api-php-client
* https://github.com/googleapis/google-api-php-client-services
*/
require "vendor/autoload.php";
$authConfig = './client_secrets.json'; // Create and download from: https://console.cloud.google.com/apis/credentials
$redirectUri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
session_start();
$client = new Google\Client();
$client->setAuthConfig($authConfig);
$client->addScope(Google\Service\Blogger::BLOGGER);
$client->setRedirectUri($redirectUri);
if (isset($_REQUEST['logout'])) {
unset($_SESSION['token']);
}
if (isset($_GET['code'])) {
$token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
$client->setAccessToken($token);
$_SESSION['token'] = $token;
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
if (!empty($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
if ($client->isAccessTokenExpired()) {
unset($_SESSION['token']);
}
} else {
$authUrl = $client->createAuthUrl();
echo "<a href='{$authUrl}'>Auth!</a>";
exit;
}
$blogger = new Google\Service\Blogger($client);
$user = $blogger->users->get('self');
echo "<h1>Blogs from '{$user->getDisplayName()}'</h1>\n";
foreach($blogger->blogs->listByUser('self')->getItems() as $blog) {
echo "{$blog->getName()} ({$blog->getUrl()}) <br />\n";
$posts = $blogger->posts->listPosts($blog->getId());
foreach ($posts->getItems() as $post) {
echo " - {$post->getTitle()} <br />\n";
}
echo "<br />\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment