Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook PHP API throwing exception while login

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\.\d\s+(\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();

    }
like image 524
Prappo Avatar asked Dec 02 '25 05:12

Prappo


2 Answers

Please open /home/xxxxxx/public_html/vendor/facebook/graph-sdk/src/Facebook/Http/GraphRawResponse.php on line 107 Current code:

preg_match('|HTTP/\d.\d\s+(\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.

like image 192
Kamlesh Avatar answered Dec 04 '25 01:12

Kamlesh


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.

like image 40
B. Fleming Avatar answered Dec 04 '25 00:12

B. Fleming



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!