martin1982/facebook-graph-sdk
PHP 7.4+ Facebook Graph SDK (v7) for accessing the Facebook Platform. Install via Composer, initialize with app ID/secret, and make Graph API requests with built-in helpers for login flows and access tokens. Includes docs and PHPUnit tests.
This example covers getting profile information for the current user and printing their name, using the Graph API and the Facebook SDK for PHP.
It assumes that you've already obtained an access token from one of the helpers found here.
For more information, see the documentation for Facebook\Facebook, Facebook\Response, Facebook\GraphNode\GraphUser, Facebook\Exception\SDKException and Facebook\Exception\ResponseException.
$fb = new Facebook\Facebook([
'app_id' => '{app-id}',
'app_secret' => '{app-secret}',
'default_graph_version' => 'v10.0',
]);
try {
// Returns a `Facebook\Response` object
$response = $fb->get('/me?fields=id,name', '{access-token}');
} catch(Facebook\Exception\ResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exception\SDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$user = $response->getGraphUser();
echo 'Name: ' . $user['name'];
// OR
// echo 'Name: ' . $user->getName();
How can I help you explore Laravel packages today?