A simple PHP library for posting to Facebook Pages using the Graph API. This package allows you to post text messages and photos to your Facebook page.
- PHP >= 8.2
- cURL extension
- JSON extension
- Facebook App with Page access
- Long-lived Page Access Token
composer require codechap/f
composer require codechap/f @devRun the setup script to configure your Facebook page:
php setup.phpThis will guide you through:
- Getting an access token from Facebook
- Selecting your Facebook page
- Saving the configuration
<?php
require 'vendor/autoload.php';
use Codechap\F\F;
use Codechap\F\Msg;
// Initialize Facebook client
$fb = new F();
$fb->set('pageId', 'YOUR_PAGE_ID');
$fb->set('accessToken', 'YOUR_PAGE_ACCESS_TOKEN');
// Create a simple text post
$msg = new Msg();
$msg->set('content', 'Hello Facebook!');
$response = $fb->post($msg);
echo "Posted! ID: " . $response['id'];$msg = new Msg();
$msg->set('content', 'Hello from PHP!');
$fb->post($msg);$msg = new Msg();
$msg->set('content', 'Check out this photo!');
$msg->set('image', 'path/to/photo.jpg');
$fb->post($msg);$photo1 = new Msg();
$photo1->set('image', 'photo1.jpg');
$photo2 = new Msg();
$photo2->set('image', 'photo2.jpg');
$photo3 = new Msg();
$photo3->set('content', 'Photo gallery');
$photo3->set('image', 'photo3.jpg');
$fb->post([$photo1, $photo2, $photo3]);$info = $fb->me();
echo "Page: " . $info['data']['name'];php setup.phpFollow the prompts to get your token from Facebook Graph API Explorer.
- Go to Facebook Graph API Explorer
- Select your app from the dropdown
- Click "User or Page" → "Get User Token"
- Add these permissions:
pages_show_listpages_manage_postspages_read_engagementpublic_profile
- Click "Generate Access Token"
- Grant permissions and select your pages
- Copy the token
To get a long-lived token (60+ days):
- Go to Access Token Debugger
- Paste your token
- Click "Extend Access Token"
- Use the extended token in your code
This package is configured to work with an app:
- App ID:
YOUR_APP_ID_HERE - App Secret:
YOUR_APP_SECRET_HERE
You can also use your own Facebook app by updating these values in setup.php.
Main class for Facebook operations.
set(string $key, string $value)- Set configuration (pageId, accessToken, apiVersion)post($content)- Post content to Facebook (accepts Msg or array of Msg)me()- Get page information
Message content handler.
set(string $key, string $value)- Set content or image pathget(string $key)- Get content or image pathhasContent()- Check if message has texthasImage()- Check if message has an image
After running setup.php, your configuration is saved to .env.local:
FACEBOOK_PAGE_ID=YOUR_PAGE_ID
FACEBOOK_PAGE_NAME="Your Page Name"
FACEBOOK_ACCESS_TOKEN=YOUR_TOKEN
FACEBOOK_APP_ID=YOUR_APP_ID
FACEBOOK_APP_SECRET=YOUR_APP_SECRET_HEREtry {
$fb->post($msg);
} catch (\RuntimeException $e) {
// Facebook API errors
echo "API Error: " . $e->getMessage();
} catch (\Exception $e) {
// Other errors
echo "Error: " . $e->getMessage();
}- Maximum size: 10MB
- Supported formats: JPEG, PNG, GIF, WEBP
- Minimum dimensions: 200x200 pixels
Facebook enforces rate limits on API calls. Be mindful of:
- Posting frequency
- Number of API calls per hour
- Batch operations when posting multiple items
- Never commit access tokens to version control
- Use environment variables or
.env.localfiles - Add
.env.localto.gitignore - Regenerate tokens periodically
- Keep app credentials secure
- Make sure you're an admin of at least one Facebook page
- Grant all required permissions when generating the token
- Select your pages in the authorization popup
- Note: Pages belonging to different Business Manager accounts or business assets may not all appear together. You can only access pages within the same business context in a single token
- Tokens expire after 60 days
- Run
setup.phpagain to get a new token - Consider implementing automatic token refresh
- Ensure your app has the required permissions
- Regenerate token with all permissions granted
- Check that your page is published and accessible
MIT License - see LICENSE file for details.