A simple yet powerful PHP library for automating Facebook Page posts. Supports manual posting from local JSON/images as well as AI-powered content generation using Google Gemini. Designed for developers who want a clean, flexible, and cron frndly solution for Facebook automation.
Features β’ Installation β’ Usage Examples β’ Troubleshooting β’ Contributing
- π Manual posts (JSON + Images)
- π€ AI-generated content
- π¨ AI images with captions
- πΈ Only Image posts
- βοΈ Only Caption posts
- PHP 7.4 or higher
- cURL extension
- Facebook Access Token
- Gemini KEY (for AI features)
Iβm not going to tell you how to install it.
If you canβt figure it out yourself, then fcuk uπThis library is for people who know what theyβre doingπͺ
- Go to your Facebook Page
- Click "About"
- Scroll down to find "Page ID"
- Go to Facebook Developers
- Create an app (if you don't have one)
- Add "Facebook Login" product
- Go to Graph API Explorer
- Select your app
- Generate token with these permissions:
pages_show_listpages_read_engagementpages_manage_posts
- Click "Generate Access Token"
- Copy the token
Your token expires in 1 hour by default. So get a long-lived token:
<?php
$app_id = 'APP_ID';
$app_secret = 'APP_SECRET';
$short_token = 'ACCESS_TOKEN';
$url = "https://graph.facebook.com/oauth/access_token?" .
"grant_type=fb_exchange_token&" .
"client_id={$app_id}&" .
"client_secret={$app_secret}&" .
"fb_exchange_token={$short_token}";
$response = file_get_contents($url);
$data = json_decode($response, true);
echo "Long-lived token: " . $data['access_token'];
?><?php
require_once 'facebookAutoPoster.php';
$config = [
'page_id' => 'PAGE_ID',
'page_access_token' => 'PAGE_ACCESS_TOKEN',
'image_folder' => './images/',
'caption_file' => './captions.json',
'ai_use' => 2, // 1 = AI Mode, 2 = Manual Mode
'gemini_api_key' => 'GEMINI_KEY',
'log_file' => './logs/facebook_poster.log',
'facebook_api_version' => 'v20.0'
];
$poster = new FacebookAutoPoster($config);
$result = $poster->post();
echo json_encode($result, JSON_PRETTY_PRINT);π‘ Pro Tip: Make sure your
images/andlogs/directories are writable!
$config['ai_use'] = 2;
$poster = new FacebookAutoPoster($config);
$result = $poster->post();
print_r($result);π Note: Place your images in ./images/ and captions in captions.json
$config['ai_use'] = 1;
$ai_poster = new FacebookAutoPoster($config);
$result = $ai_poster->post([
'post_type' => 'image_caption',
'prompt' => 'A beautiful sunset over mountains with vibrant colors'
]);$result = $ai_poster->post([
'post_type' => 'caption_only',
'prompt' => 'Motivational quote about success and perseverance'
]);$result = $ai_poster->post([
'post_type' => 'image_only',
'prompt' => 'Abstract digital art with vibrant neon shapes'
]);Create cron.php:
<?php
require_once 'facebookAutoPoster.php';
$poster = new FacebookAutoPoster([
'page_id' => 'PAGE_ID',
'page_access_token' => 'PAGE_ACCESS_TOKEN',
'ai_use' => 2,
'image_folder' => '/images/',
'caption_file' => '/captions.json'
]);
$result = $poster->post();
file_put_contents('./logs/cron.log',
date('Y-m-d H:i:s') . ' - ' . json_encode($result) . PHP_EOL,
FILE_APPEND
);Cron Schedule Examples:
# π Every hour
0 * * * * /usr/bin/php /fbauto/cron.php
# π 3 times daily (9 AM, 3 PM, 9 PM)
0 9,15,21 * * * /usr/bin/php /fbauto/cron.php
# π Once daily at midnight
0 0 * * * /usr/bin/php /fbauto/cron.php// Facebook Token Test
$poster->testFacebookConnection();
// Gemini Key Test
$poster->testGeminiConnection();
// Get Available images
$images = $poster->getAvailableImages();
// Count remaining captions
$count = $poster->getRemainingCaptionsCount();
// Current configuration
$configs = $poster->getConfig();Every method returns a standardized JSON response:
{
"status": "success",
"message": "Post published successfully!",
"timestamp": "2025-10-03 12:00:00",
"post_id": "123456789_987654321",
"post_type": "image_caption"
}β Invalid Token Error
Problem: Invalid OAuth access token
Solution:
- Verify your Page Access Token is valid
- Ensure it has
pages_manage_postspermission - Regenerate token if expired
π Permission Denied
Problem: Can't write to files or directories
Solution:
chmod 755 images/
chmod 755 logs/
chmod 644 captions.jsonβ±οΈ Rate Limit Exceeded
Problem: Too many requests to Facebook API
Solution:
- The library retries automatically
- Reduce posting frequency in cron
- Wait a few minutes before retrying
π No Captions Available
Problem: captions.json is empty or invalid
Solution:
- Verify JSON format is correct
- Add more captions to the file
- Check file permissions
- Store tokens in
.envfiles (never commit them!) - Use environment variables for sensitive data
- Set proper file permissions (755 for dirs, 644 for files)
- Test in development before production
- Monitor logs regularly
- Rotate API keys periodically
This project is licensed under the MIT License - feel free to use, modify, and share! π
Contributions are welcome! Here's how you can help:
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Requestπ―
Found a bug? π Open an issue