PHP Facebook SDK - 异常 - 捕获错误代码(没有消息)

时间:2014-09-12 10:54:40

标签: php facebook facebook-graph-api exception

我想从Facebook的异常消息中获取错误代码 - 不做一些hacky爆炸/解决方法。我的代码:

try {
    // some fb api method
} catch (Exception $e) {
    echo $e; // Outputs i.e. "OAuthException: (#200) Cannot send notifications to a user who has not installed the app"
    echo $e->getCode(); // Ouputs "0". Expected: "200"
}

我注意到,通过执行var_dump($e)对象确实包含代码 - 但我不知道如何捕获它,因为它受到保护。

仅供参考,这就是$e的样子:

FacebookApiException Object
(
    [result:protected] => Array
        (
            [error] => Array
                (
                    [message] => (#100) Must specify a non-empty template param
                    [type] => OAuthException
                    [code] => 100
                )

        )

    [message:protected] => (#100) Must specify a non-empty template param
    [string:Exception:private] => 
    [code:protected] => 0
    [file:protected] => /data/home/my_project/public_html/php-sdk/base_facebook.php
    [line:protected] => 1271
    [trace:Exception:private] => Array
        (
            [0] => Array
                (
                    [file] => /data/home/my_project/public_html/php-sdk/base_facebook.php
                    [line] => 880
                    [function] => throwAPIException
                    [class] => BaseFacebook
                    [type] => ->
                    [args] => Array
                        (
                            [0] => Array
                                (
                                    [error] => Array
                                        (
                                            [message] => (#100) Must specify a non-empty template param
                                            [type] => OAuthException
                                            [code] => 100
                                        )

                                )

                        )

                )

            [1] => Array
                (
                    [function] => _graph
                    [class] => BaseFacebook
                    [type] => ->
                    [args] => Array
                        (
                            [0] => /fb_user_id/notifications
                            [1] => POST
                            [2] => Array
                                (
                                    [template] => 
                                    [href] => transfer=
                                    [access_token] => my_app_id|my_app_secret
                                )

                        )

                )

            [2] => Array
                (
                    [file] => /data/home/my_project/public_html/php-sdk/base_facebook.php
                    [line] => 654
                    [function] => call_user_func_array
                    [args] => Array
                        (
                            [0] => Array
                                (
                                    [0] => Facebook Object
                                        (
                                            [sharedSessionID:protected] => 
                                            [appId:protected] => my_app_id
                                            [appSecret:protected] => my_app_secret
                                            [user:protected] => 
                                            [signedRequest:protected] => 
                                            [state:protected] => 
                                            [accessToken:protected] => 
                                            [fileUploadSupport:protected] => 
                                            [trustForwarded:protected] => 
                                        )

                                    [1] => _graph
                                )

                            [1] => Array
                                (
                                    [0] => /fb_user_id/notifications
                                    [1] => POST
                                    [2] => Array
                                        (
                                            [template] => 
                                            [href] => transfer=
                                            [access_token] => my_app_id|my_app_secret
                                        )

                                )

                        )

                )

            [3] => Array
                (
                    [file] => /data/home/my_project/public_html/core/controllers/edu-cron.php
                    [line] => 524
                    [function] => api
                    [class] => BaseFacebook
                    [type] => ->
                    [args] => Array
                        (
                            [0] => /fb_user_id/notifications
                            [1] => POST
                            [2] => Array
                                (
                                    [template] => 
                                    [href] => transfer=
                                    [access_token] => my_app_id|my_app_secret
                                )

                        )

                )

        )

    [previous:Exception:private] => 
)

1 个答案:

答案 0 :(得分:3)

你也试过这个吗?

$result = $e->getResult(); //which is the 1st method of the class and provide you all response 
//so for getting the code only 

echo $result['error']['code'];

我无法回答评论。