3

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();

    }
5
  • Please show us the actual error you're encountering. We can't help you if we don't even know what the error is. For all we know, you could have forgotten to make some important changes in the Facebook developer console after receiving a warning. It could even be a simple matter of not having the correct domain name. It could be just about anything.
    – B. Fleming
    Commented Nov 15, 2018 at 20:48
  • The error message is "Undefined offset: 1"
    – Prappo
    Commented Nov 15, 2018 at 20:59
  • 1
    preg_match must be failing to parse the header for the value you're looking for. It's very likely that they've changed the format of their header since you first wrote this code. It looks to me like you're using the SDK, so why not use the appropriate SDK method getHttpStatusCode() mentioned here?: developers.facebook.com/docs/php/FacebookResponse/…
    – B. Fleming
    Commented Nov 15, 2018 at 21:25
  • @B.Fleming: “It's very likely that they've changed the format of their header since you first wrote this code” - this part of the SDK itself; and I somehow doubt Facebook has changed the format of the HTTP status code line by themselves ;-) They probably did not write “defensive” code at this point, assuming that there must always be an HTTP status code with every API response. Things have probably failed in some way with the HTTP request itself for that not to be the case.
    – misorude
    Commented Nov 16, 2018 at 8:07
  • @misorude You're right, I misread the code the first time through. After reviewing, it's possible that the issue could be related to this: github.com/facebook/php-graph-sdk/issues/1076
    – B. Fleming
    Commented Nov 16, 2018 at 19:32

4 Answers 4

16

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.

2
  • can you please explain, what is this supposed to achieve? I understand it does some escaping and removes the dot from the matching string at the end, but what is the reasoning behind this?
    – Kalko
    Commented Feb 23, 2022 at 10:13
  • Strangely, but this worked! The bad thing is that I don't want to change something in the Vendor folder, since it will be overwritten by the next update or will be lost when I switch the server. Commented Jul 2, 2023 at 5:57
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.

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

1
  • I'm using the latest version of PHP graph SDK. it's 5.6
    – Prappo
    Commented Nov 16, 2018 at 6:26
0

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.