doupin5667 2013-08-07 08:19
浏览 32
已采纳

抛出APIException Facebook api php

I'd like to get my api exception but I don't understand how to do that. This is my code

public function facebook($link){

    if(!$link || !trim($link) != ""){
        return false;
    }

    $config = array(
            'appId'=>$this->keys['facebook']['id'],
            'secret'=>$this->keys['facebook']['secret'],
            'fileUpload'=>false
    );

    $facebook = new Facebook($config);


    $start = strrpos($link, '/', -1);
    $end = strripos($link, '?', -1);
    $end = ($end)?$end:strlen($link);

    $pageId = ($end == strlen($link))?substr($link, $start + 1):substr($link, $start + 1, $end - strlen($link));

    try {
        $pagefeed = $facebook->api("/" . $pageId . "/feed");
    }
    catch (FacebookApiException $e){
        return false;
    }

    //set datetime
    foreach ($pagefeed['data'] as $key => $post){
        $pagefeed['data'][$key]['datetime'] = new \DateTime($post['created_time']);
    }

    return $pagefeed;
}

So I'd like to return false in case of exception.

I got for exemple:

BaseFacebook ->throwAPIException (array('error' => array('message' => '(#803) Some of the aliases you requested do not exist: lkdsgfkqdjgflkdshbf', 'type' => 'OAuthException', 'code' => '803'))) 

Thanks for your help

  • 写回答

1条回答 默认 最新

  • dounuo9921 2013-08-07 08:38
    关注

    Since you've commented that you're using symfony, and you fixed the type-hint using catch(\Exception $e) you might want to consider adding this to the top of your file:

    use \APIException;
    

    To set APIException as an alias to \APIException. Also check this link. Not having used the FB API, I don't know if it's still relevant, but assuming the facebook api is stored in your vendor dir, you'll have to specify the correct namespaces when using the Facebook api.
    The reason why \Exception worked is simply because, as the linked page shows, the APIException class extends from the \Exception base-class, so the type-hint works. It's not crucial, but it's generally better to catch the right exception in the right place.

    The Exception is thrown, and you catch it using the catch block. So far so good, it is, though, caught in the method's scope, which is GC'ed when that method returns (Garbage Collected). The Exception isntance doesn't exits anymore.
    Generally, if you want to access the exception outside of the method (most likely in the code that invoked the method), you just don't catch the exception. Remove the try-catch from the facebook method and do this:

    //call method:
    try
    {
        $return = $instance->facebook($someLink);
    }
    catch (APIException $e)
    {
        $return = false;//exception was thrown, so the return value should be false
        var_dump($e);//you have access to the exception here, too
    }
    

    Catching an exception and not doing anything with it (you're catching but returning false, no way of knowing why) is considered bad practice.
    If you want to avoid wrapping all those calls to your facebook method in a try-catch, you could do something like this, too:

    //in class containing facebook method:
    private $lastException = null;
    public function getLastException()
    {
        return $this->lastException;
    }
    

    Now you can change your facebook method's catch block to:

    catch(APIException $e)
    {
        $this->lastException = $e;
        return false;
    }
    

    And do something like:

    $return = $instance->facebook($link);
    if ($return === false)
    {
        var_dump($instance->getLastException());
        exit($instance->getLastException()->getMessage());
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100