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
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
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:
These are given in order of most preferred to least preferred, with stability and reliability being the primary concern.
Please open /home/xxxxxx/public_html/vendor/facebook/graph-sdk/src/Facebook/Http/GraphRawResponse.php on line 107
Current code:
You can edit the code to:
This worked for me like charm. I hope, this will also solve your problem. Thanks for asking this beautiful question.
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