I have a project to connect facebook with api and show clients information on my site. I have app id, app secret and access token already, and build code in laravel. I use facebook-php-business-sdk module to access to facebook. But when run the code, "$response = $fb->get(" code occurs "Undefined array key 1" error. And then didn't go to the next code. Why this error occurs? And how can I do to get data?
This is my code.
public function getFacebookInfo($name)
{
$access_token = env('YOUR_ACCESS_TOKEN', 'YOUR_ACCESS_TOKEN');
$app_id = env('YOUR_APP_ID', 'YOUR_APP_ID');
$app_secret = env('YOUR_APP_SECRET', 'YOUR_APP_SECRET');
// Initialize the Facebook SDK
$fb = new Facebook([
'app_id' => $app_id,
'app_secret' => $app_secret,
'default_graph_version' => 'v13.0',
]);
try {
// Get the user profile information
$response = $fb->get('/' . $name . '?fields=id,username,full_name,profile_picture_url,biography,follows_count,followed_by_count,media_count,is_private', $access_token);
$userInfo = $response->getGraphNode()->asArray();
$data = [
'user-info' => $userInfo
];
return $data;
} catch (FacebookResponseException $e) {
// Handle Facebook API errors
return response()->json(['error' => $e->getMessage()], 500);
} catch (FacebookSDKException $e) {
// Handle SDK errors
return response()->json(['error' => $e->getMessage()], 500);
}
}
User
object type, so that request will result in errors. And if$name
was supposed to actually be a user name, then it will also fail, because accessing user profiles by user name has been removed a long time ago. Or are you actually trying to access pages here, not user profiles? But even then you'd still be asking for fields that don't exist there either.facebook/facebook-php-business-sdk
butfacebookarchive/php-graph-sdk
and it's archived and perhaps not working anymore, i can see that this package won't work with php 8. And for the package you mentioned it's for ads, i don't think you will be able to retrieve user data with it.