【问题标题】:exception handling with PHP and facebook使用 PHP 和 facebook 处理异常
【发布时间】:2014-04-01 17:35:36
【问题描述】:

我有以下代码:

$fql = "SELECT first_name, last_name FROM user where uid=me()";
$sparam = array('method' => 'fql.query', 'query' => $fql, 'callback' => '', 'access_token' => $fbtoken);
try{
    $sfqlResult = $facebook -> api($sparam);
}
catch(CurlException $e) {
    echo 'There is issue with Token';
}

假设如果我的访问令牌错误,我想捕获异常,但它没有捕获异常并显示传统错误

致命错误:未捕获异常:190:OAuth 访问令牌无效。扔进去

这方面的任何帮助。

【问题讨论】:

    标签: php facebook facebook-graph-api facebook-fql facebook-php-sdk


    【解决方案1】:

    您可以使用FacebookApiException Class 来捕获异常。

    例子-

    try {
      $sfqlResult = $facebook -> api($sparam);
    } catch(FacebookApiException $e) {
      $e_type = $e->getType();
      $result = $e->getResult();
      error_log('Got an ' . $e_type . ' while posting');
      error_log(json_encode($result));
    }
    


    但据我所知,有令牌相关的问题,不会抛出异常但错误结果会在$sfqlResult中返回;你可以检查对象$sfqlResult->error;

    if(!isset($sfqlResult['error']))
    {
        // no error
    }
    else
    {
        echo "Facebook error: ".$sfqlResult['error']->message;
    }
    

    【讨论】:

    • 如果它的访问令牌相关问题,异常不会被抛出 acc。您将在$sfqlResult 中收到错误响应
    • 它抛出致命错误:未捕获的异常:190:无效的 OAuth 访问令牌。投入
    • 我认为您应该首先关注错误。我提到的代码主要处理所有事情。您是否使用正确的应用 ID 和应用密码正确生成了令牌?
    • 我知道,但只是考虑一个案例,如果令牌错误,我的用户会看到什么
    猜你喜欢
    • 2012-09-15
    • 2011-02-18
    • 2014-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多