1. Go to this page and download the library: Download horde/service_facebook library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
horde / service_facebook example snippets
use Horde\Http\Client;
use Horde\Http\RequestFactory;
use Horde\Http\StreamFactory;
use Horde\OAuth\Client\AuthenticatedHttpClient;
use Horde\OAuth\Client\TokenSet;
use Horde\Service\Facebook\FacebookApiClient;
// Bearer-authenticated PSR-18 client.
$tokenSet = new TokenSet(
accessToken: $userAccessToken,
tokenType: 'Bearer',
expiresAt: null,
refreshToken: null,
scope: null,
);
$httpClient = new AuthenticatedHttpClient(new Client(), $tokenSet);
$fb = FacebookApiClient::create(
httpClient: $httpClient,
requestFactory: new RequestFactory(),
streamFactory: new StreamFactory(),
);
$me = $fb->getMe(['id', 'name', 'email']);
echo $me->name; // "Ada Lovelace"
echo $me->email; // "[email protected]" (if the token holds the email scope)
use Horde\Service\Facebook\Graph\GraphApiVersion;
$fbV24 = $fb->withVersion(GraphApiVersion::V24_0);
$events = iterator_to_array($fbV24->listMyUpcomingEvents());
use Horde\Service\Facebook\Graph\Pagination\Cursor;
foreach ($fb->listMyUpcomingEvents(new Cursor(limit: 25)) as $event) {
echo $event->name, ' @ ', $event->startTime->format('Y-m-d H:i'), "\n";
}
foreach ($fb->listMyPermissions() as $perm) {
if (!$perm->isGranted()) {
echo $perm->name(), ' status: ', $perm->status(), "\n";
}
}
$info = $fb->debugToken($someUserToken, $appId . '|' . $appSecret);
if (!$info->isValid()) {
// Reject the request. The caller's token is no good.
}
if ($info->expiresAt !== null && $info->expiresAt <= new DateTimeImmutable('+5 minutes')) {
// Nearly expired. Trigger a refresh.
}
use Horde\OAuth\Client\OAuth2Client;
use Horde\Service\Facebook\Graph\GraphApiVersion;
use Horde\Service\Facebook\OAuth\FacebookProviderConfig;
$providerConfig = FacebookProviderConfig::forVersion(GraphApiVersion::V25_0);
$oauth = new OAuth2Client(
$httpClient,
$requestFactory,
$streamFactory,
$providerConfig,
clientId: getenv('FB_APP_ID'),
clientSecret: getenv('FB_APP_SECRET'),
);
// 1. Redirect the user to Facebook.
$state = bin2hex(random_bytes(16));
$authUrl = $oauth->getAuthorizationUrl(
redirectUri: 'https://example.org/callback',
scopes: ['public_profile', 'email', 'user_events'],
state: $state,
);
header('Location: ' . $authUrl);
// 2. In the callback handler, exchange the code for a TokenSet.
$tokenSet = $oauth->exchangeCode($_GET['code']);
// 3. Store $tokenSet->accessToken (and $tokenSet->refreshToken if present).
// 4. Wrap your PSR-18 client and hand it to FacebookApiClient::create().
use Horde\Service\Facebook\OAuth\FacebookOidcSupport;
use Horde\Service\Facebook\OAuth\FacebookProviderConfig;
$oidc = new FacebookOidcSupport($httpClient, $requestFactory);
$verified = $oidc->verifyIdToken($idToken, [
'verify_iss' => FacebookProviderConfig::ISSUER,
'verify_aud' => getenv('FB_APP_ID'),
]);
$userId = $verified->getSubject();
$claims = $verified->getClaims();
use Horde\Service\Facebook\Graph\GraphErrorException;
try {
$events = iterator_to_array($fb->listMyUpcomingEvents());
} catch (GraphErrorException $e) {
if ($e->error()->isTokenExpired()) {
// Refresh the token, then retry.
}
if ($e->error()->isRateLimit()) {
// Back off. Check e->error()->isAppRateLimit() vs isUserRateLimit()
// for the specific limit.
}
if ($e->error()->isPermissionMissing()) {
// Ask the user to grant the missing scope.
}
throw $e;
}
use Horde\Service\Facebook\Graph\Value\Event;
// Version-portable. Accepts any V{N}\Value\Event.
function renderEventSummary(Event $event): string
{
return $event->name() . ' @ ' . $event->startTime()->format('Y-m-d H:i');
}
use Horde\Service\Facebook\Graph\GraphApiVersion;
use Horde\Service\Facebook\Graph\ProfileUrls;
// Link to a user's public profile page.
$link = ProfileUrls::profileLink('12345');
// => https://www.facebook.com/12345
$link = ProfileUrls::profileLink('zuck');
// => https://www.facebook.com/zuck
// Profile picture URL. Meta serves a 302 to the actual image.
$thumb = ProfileUrls::thumbnailUrl('12345');
// => https://graph.facebook.com/v25.0/12345/picture
// With size/type options.
$thumb = ProfileUrls::thumbnailUrl('12345', GraphApiVersion::V25_0, [
'type' => 'large',
]);
$thumb = ProfileUrls::thumbnailUrl('12345', null, [
'width' => 200,
'height' => 200,
]);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.