Facebook php sdk显示错误

Facebook php sdk showing error

本文关键字:错误 显示 sdk php Facebook      更新时间:2023-09-26

有时会出现这个错误

致命错误:Uncaught GraphMethodException:不支持get请求。在第1039行抛出base_facebook.php

这就是我显示用户名的方式。这里少了什么东西吗?

  $user_id = $facebook->getUser();
  $userInfo = $facebook->api("/$user_id");
  $accessToken = $_SESSION['fb_sdfsdfsdfsd_access_token'];
  echo  $userInfo['name']."<br />";
  $uid = $userInfo['id'];

您可能需要包含更多的代码,以确保您确实为用户提供了一个有效的会话。

试试这样写:

$user_id=null;
try{
  $user_id = $facebook->getUser();
}catch(Exception $e){
  //could help if you log this error
}
if($user_id){
  try{
    $user_profile = $facebook->api('/me?fields=name,gender,locale'); //I just ask for specific fields
  }catch(FacebookApiException $e){
    //could help if you log this error
    $user_id = null;
  }
}
$loginUrl = $facebook->getLoginUrl(array('redirect_uri' => ''));
if (!$user_id) {
  echo '<script type="text/javascript">top.location.href = "'. $loginUrl .'";</script>';
  exit;
}
//FYI: How I get the access_token
$access_token = $facebook->getAccessToken();