Skip to content

ThirteeNov

My personal blog about coding and internet

Menu
  • About me
  • About Zofia Kreasi
  • Cart
  • Checkout
  • Making an airplane game from scratch in Unity
  • My account
  • Privacy Policy
  • Privacy Policy – zkLeaderboard
  • Sample Page
  • Shop
  • Tutorials on Learning JavaScript
  • ZKAccounts – Privacy Policy
Menu

Simple HTML and PHP program to submit a blog post to Google’s Blogger using its API

Posted on March 20, 2025 by Habibie

Here’s an HTML + PHP implementation to submit a blog post to Blogger using the Google Blogger API.

You’ll need:

  1. A Google API key and OAuth 2.0 Client ID & Secret.
  2. Your Blogger blog ID.
  3. OAuth authentication to authorize API requests.

Steps:

  1. Set up Google OAuth Credentials:
    • Go to Google Cloud Console.
    • Create a project, enable the Blogger API v3, and generate OAuth credentials.
    • Set up redirect URI in your OAuth client (e.g., http://localhost/callback.php).
  2. Use the following files:
    • index.html → Blog post submission form.
    • authenticate.php → OAuth authentication.
    • callback.php → Retrieve access token.
    • post.php → Submit the blog post.

index.html (Front-end Form)

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Blogger Post Submission</title>
</head>
<body>
<h2>Submit a Blog Post</h2>
<form action="post.php" method="post">
<label>Title:</label><br>
<input type="text" name="title" required><br><br>

<label>Content:</label><br>
<textarea name="content" rows="5" required></textarea><br><br>

<button type="submit">Submit Post</button>
</form>
</body>
</html>

authenticate.php (OAuth Authentication)

<?php
session_start();

$client_id = "YOUR_CLIENT_ID";
$redirect_uri = "http://localhost/callback.php";
$scope = "https://www.googleapis.com/auth/blogger";

$auth_url = "https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=$client_id&redirect_uri=$redirect_uri&scope=$scope&access_type=offline";

header("Location: $auth_url");
exit();
?>

callback.php (Retrieve Access Token)

<?php
session_start();

$client_id = "YOUR_CLIENT_ID";
$client_secret = "YOUR_CLIENT_SECRET";
$redirect_uri = "http://localhost/callback.php";

if (isset($_GET['code'])) {
$code = $_GET['code'];

$token_url = "https://oauth2.googleapis.com/token";
$post_data = [
"code" => $code,
"client_id" => $client_id,
"client_secret" => $client_secret,
"redirect_uri" => $redirect_uri,
"grant_type" => "authorization_code"
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $token_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = json_decode(curl_exec($ch), true);
curl_close($ch);

if (isset($response['access_token'])) {
$_SESSION['access_token'] = $response['access_token'];
echo "Authentication successful! You can now post to Blogger.";
} else {
echo "Authentication failed.";
}
}
?>

post.php (Submit Blog Post)

<?php
session_start();

if (!isset($_SESSION['access_token'])) {
die("Error: Not authenticated. <a href='authenticate.php'>Login with Google</a>");
}

$access_token = $_SESSION['access_token'];
$blog_id = "YOUR_BLOGGER_BLOG_ID";

if ($_SERVER["REQUEST_METHOD"] === "POST") {
$title = $_POST["title"];
$content = $_POST["content"];

$api_url = "https://www.googleapis.com/blogger/v3/blogs/$blog_id/posts/";

$post_data = json_encode([
"kind" => "blogger#post",
"title" => $title,
"content" => $content
]);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: Bearer $access_token",
"Content-Type: application/json"
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);

$result = json_decode($response, true);

if (isset($result['id'])) {
echo "Post published successfully! <a href='{$result['url']}' target='_blank'>View Post</a>";
} else {
echo "Failed to publish post.";
}
}
?>

How It Works:

  1. User visits index.html and enters a blog post title & content.
  2. If not authenticated, they are redirected to authenticate.php to log in via Google OAuth.
  3. Google redirects back to callback.php, where an access token is retrieved and stored in a session.
  4. User submits the post from index.html, which sends data to post.php.
  5. post.php sends an API request to Blogger, publishing the post.

Notes:

  • Replace YOUR_CLIENT_ID, YOUR_CLIENT_SECRET, and YOUR_BLOGGER_BLOG_ID with actual values.
  • The access token expires after some time; you may need to refresh it using a refresh token.
  • Set up OAuth redirect URI properly in Google Cloud Console.
  • Ensure cURL is enabled in your PHP installation.

This setup allows users to authenticate with Google and submit blog posts to Blogger using PHP.

Post Views: 446
ciihuy2020

Welcome!

  • My YouTube Channel
  • My GitHub Page
  • About me

Categories

  • 3DVista
  • Android
  • Apache
  • C#
  • Cordova
  • Electron & Node JS
  • HTML5, CSS & JavaScript
  • iOS
  • Let's Make Unity Games
  • Misc
  • Photoshop
  • PHP
  • Python
  • Uncategorized
  • Unity
  • WordPress

Recent Posts

  • Make objects like wires and cables easily in Unity using Ciihuy Curved Mesh
  • [SOLVED] Can’t Add Custom Domain to Blogger After Losing CNAME Verification
  • iOS App Icon Generator by CiihuyCom
  • Advanced Blinking Marker Script to show objects position in your game canvas
  • Ciihuy Images Merger – Fast & Easy Online Image Combiner
© 2025 ThirteeNov | Powered by Superbs Personal Blog theme