Skip to content
PHP Logo

How can you determine if a user has liked a specific Facebook page using the Facebook API in PHP?

To determine if a user has liked a specific Facebook page using the Facebook API in PHP, you can make a request to the Graph API endpoint for the user's likes and check if the specific page is included in the response data. You will need to have the user grant permission to access their likes using the user_likes permission.

php
// Make a request to the Graph API endpoint for the user's likes
$accessToken = 'YOUR_ACCESS_TOKEN';
$userId = 'USER_ID';
$pageId = 'PAGE_ID';

$response = file_get_contents("https://graph.facebook.com/v12.0/$userId/likes?access_token=$accessToken");

// Check if the specific page is included in the response data
$likes = json_decode($response, true);
$likedPages = array_column($likes['data'], 'id');

if (in_array($pageId, $likedPages)) {
    echo 'User has liked the page';
} else {
    echo 'User has not liked the page';
}