skip to Main Content

I tried to log in with Facebook Graph API and get user info. The code I used to get user info worked before but today I tried login with facebook but Facebook API throwing this error.

Undefined offset: 1
/home/****/vendor/facebook/php-sdk-v4/src/Facebook/Http/GraphRawResponse.php on line 108

The line is in this function:

public function 
setHttpResponseCodeFromHeader($rawResponseHeader)
{
    preg_match('|HTTP/d.ds+(d+)s+.*|', $rawResponseHeader, $match);
    $this->httpResponseCode = (int)$match[1]; // <---- HERE
}

My Code :

    $fb = new Facebook([
        'app_id' => Data::get('fbAppId'),
        'app_secret' => Data::get('fbAppSec'),
        'default_graph_version' => 'v2.5',
    ]);

    $helper = $fb->getRedirectLoginHelper();
    $_SESSION['FBRLH_state'] = $_GET['state'];

    try {
        $accessToken = $helper->getAccessToken();
        $_SESSION['token'] = $accessToken;
        DB::table('settings')->where('userId', Auth::user()->id)->update(['fbAppToken' => $accessToken]); // save user access token to database
        $this->saveFbPages(); // save facebook pages and token
        $this->saveFbGroups(); // save facebook groups to database

    } catch (FacebookResponseException $e) {
        // When Graph returns an error
        return '[a] Graph returned an error: ' . $e->getMessage();

    } catch (FacebookSDKException $e) {
        // When validation fails or other local issues
        return '[a] Facebook SDK returned an error: ' . $e->getMessage();

    }

4

Answers


  1. I think you’re using an outdated version of the Facebook SDK (php-sdk-v4), you should be using version 5.

    https://github.com/facebook/php-graph-sdk

    Login or Signup to reply.
  2. This appears to be a known issue in the PHP graph SDK. This issue had a fix applied only two days ago, as can be seen in the GitHub issues on its repo. The last release, on the other hand, was in early July, so this fix is currently unavailable in the most current release version of the SDK.

    You have a few options available to you:

    1. You could try downgrading your version of curl used by PHP.
    2. If you’re willing to run a potentially unstable version of the SDK, you could look into updating to the master branch rather than a release version.
    3. You could apply a hotfix matching the fix that was committed to the repo.

    These are given in order of most preferred to least preferred, with stability and reliability being the primary concern.

    Login or Signup to reply.
  3. Please open /home/xxxxxx/public_html/vendor/facebook/graph-sdk/src/Facebook/Http/GraphRawResponse.php on line 107
    Current code:

    preg_match('|HTTP/d.ds+(d+)s+.*|',$rawResponseHeader, $match);
    

    You can edit the code to:

    preg_match('/HTTP/d(?:.d)?s+(d+)s+/',$rawResponseHeader, $match);
    

    This worked for me like charm. I hope, this will also solve your problem. Thanks for asking this beautiful question.

    Login or Signup to reply.
  4. the problem now is that facebook marked the PHP SDK as "archived" .
    Consider use this package to login: https://github.com/thephpleague/oauth2-facebook. It support PHP 8

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search