登录时Facebook PHP API抛出异常 [英] Facebook PHP API throwing exception while login
问题描述
我尝试使用Facebook Graph API登录并获取用户信息.我以前用来获取用户信息的代码之前可以运行,但是今天我尝试使用Facebook登录,但是Facebook API抛出此错误.
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.
未定义偏移量:1 /home/****/vendor/facebook/php-sdk-v4/src/Facebook/Http/GraphRawResponse.php在第108行
Undefined offset: 1 /home/****/vendor/facebook/php-sdk-v4/src/Facebook/Http/GraphRawResponse.php on line 108
该行位于此函数中:
public function
setHttpResponseCodeFromHeader($rawResponseHeader)
{
preg_match('|HTTP/\d\.\d\s+(\d+)\s+.*|', $rawResponseHeader, $match);
$this->httpResponseCode = (int)$match[1]; // <---- HERE
}
我的代码:
$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();
}
推荐答案
请在第107行打开/home/xxxxxx/public_html/vendor/facebook/graph-sdk/src/Facebook/Http/GraphRawResponse.php 当前代码:
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);
您可以将代码编辑为:
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.
这篇关于登录时Facebook PHP API抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!