-4

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);
        }
    }
3
  • The line you've mentioned doesn't seem to involve any arrays. Are you sure the error isn't from a line within the get() function perhaps?
    – ADyson
    Sep 15 at 6:30
  • More than half the fields you are asking for there, don't even exist on the 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.
    – CBroe
    Sep 15 at 6:37
  • I don't think you are using facebook/facebook-php-business-sdk but facebookarchive/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.
    – Lk77
    Sep 15 at 7:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.