Here’s an HTML + PHP implementation to submit a blog post to Blogger using the Google Blogger API.
You’ll need:
- A Google API key and OAuth 2.0 Client ID & Secret.
- Your Blogger blog ID.
- OAuth authentication to authorize API requests.
Steps:
- 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).
- 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:
- User visits
index.htmland enters a blog post title & content. - If not authenticated, they are redirected to
authenticate.phpto log in via Google OAuth. - Google redirects back to
callback.php, where an access token is retrieved and stored in a session. - User submits the post from
index.html, which sends data topost.php. post.phpsends 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.