abdulbaquee / facebook-graph-sdk
A PHP SDK to easily interact with the Facebook Graph API.
Requires
- php: >=7.4
- ext-json: *
- guzzlehttp/guzzle: ^7.0
Requires (Dev)
- mockery/mockery: ^1.4
- phpunit/phpunit: ^9.5
README
It is a PHP SDK for seamless interaction with the Facebook Graph API. It offers an intuitive interface for accessing user profiles, pages, ads, and more. Perfect for developers, it simplifies authentication, data handling, and API requests, making Facebook integration easy for PHP projects.
Simplified version of Facebook Graph SDK
Version: 1.0.0
Website: webgrapple.com
Author: abdulbaquee
Usage of Unofficial Facebook Graph SDK
This application requires the Google My Business API v4.0
1. Installation
First, install the SDK via Composer:
composer require abdulbaquee/facebook-graph-sdk
2. Basic Setup
Start by including the autoload file and initializing the SDK:
require 'vendor/autoload.php';
use Facebook\GraphSDK\OAuth;
use Facebook\GraphSDK\BaseClient;
$oauth = new OAuth('your-app-id', 'your-app-secret', 'your-redirect-uri', 'graph-version');
3. Authentication
Redirect the user to Facebook's login page to get an authorization code:
$loginUrl = $oauth->getLoginUrl(['email', 'public_profile']);
header('Location: ' . $loginUrl);
exit;
After the user authorizes, handle the callback to obtain an access token:
if (isset($_GET['code'])) {
$accessToken = $oauth->getAccessTokenFromCode($_GET['code']);
echo 'Access Token: ' . $accessToken;
}
4. Making API Requests
Use the BaseClient to make requests to the Facebook Graph API:
$client = new BaseClient($accessToken);
$response = $client->get('/me?fields=id,name,email');
$user = $response->getBody();
echo 'ID: ' . $user['id'];
echo 'Name: ' . $user['name'];
echo 'Email: ' . $user['email'];
5. Batch Requests
For multiple requests in a single call:
$batch = [
$client->createRequest('GET', '/me?fields=id,name'),
$client->createRequest('GET', '/me/friends'),
];
$batchResponse = $client->sendBatchRequest($batch);
foreach ($batchResponse->getResponses() as $response) {
print_r($response->getBody());
}
6. Error Handling
Handle errors gracefully using exceptions:
try {
$response = $client->get('/me?fields=id,name');
} catch (FacebookSDKException $e) {
echo 'Error: ' . $e->getMessage();
}
Requirements
To use the Facebook Graph SDK for PHP, ensure your environment meets the following requirements:
PHP Version
: PHP 7.4 or higherComposer
: Installed for dependency managementcURL
Extension: Enabled in your PHP environmentSSL/TLS
: Enabled for secure API communicationFacebook App
: You must have a Facebook App with a valid App ID and App Secret
Important Links
- Facebook Developer Documentation: [https://developers.facebook.com/docs/graph-api]
- Facebook App Dashboard: [https://developers.facebook.com/apps]
- Composer Installation: [https://getcomposer.org/download/]
- PHP cURL Extension: [https://www.php.net/manual/en/book.curl.php]
- GitHub Repository: [https://github.com/abdulbaquee/facebook-graph-sdk]