如何从FacebookApiException $ e获取代码ID?

时间:2014-01-24 07:00:14

标签: php facebook facebook-php-sdk

如何从FacebookApiException $e获取代码ID?

我正在使用Facebook发帖。但有时它无法发布内容。我在下面使用这些代码,我在数组$e中找到错误响应代码id,但是它们都受到保护,那我怎么能得到代码?

try{
    $facebook->setAccessToken($access_token);
    $rs= $facebook->api('/me/feed','POST',$post_data);
} catch (FacebookApiException $e) {
    print_r($e);
}

然后我收到错误信息:

FacebookApiException Object
(
    [result:protected] => Array
        (
            [error] => Array
                (
                    [message] => Invalid OAuth access token.
                    [type] => OAuthException
                    [code] => 190  // I need this code
                )

        )

    [message:protected] => Invalid OAuth access token.
    [string:private] => 
    [code:protected] => 0
    [file:protected] => D:\phpnow\htdocs\laji\facebook-php-sdk\base_facebook.php
    [line:protected] => 1267
    [trace:private] => Array
        (
            [0] => Array
                (
                    [file] => D:\phpnow\htdocs\laji\facebook-php-sdk\base_facebook.php
                    [line] => 876
                    [function] => throwAPIException
                    [class] => BaseFacebook
                    [type] => ->
                    [args] => Array
                        (
                            [0] => Array
                                (
                                    [error] => Array
                                        (
                                            [message] => Invalid OAuth access token.
                                            [type] => OAuthException
                                            [code] => 190
                                        )

                                )

                        )

                )

            [1] => Array
                (
                    [file] => D:\phpnow\htdocs\laji\facebook-php-sdk\base_facebook.php
                    [line] => 650
                    [function] => _graph
                    [class] => BaseFacebook
                    [type] => ->
                    [args] => Array
                        (
                            [0] => /me/feed
                            [1] => POST
                            [2] => Array
                                (
                                    [access_token] => 1CAACg13jYHp8BAP0QEOK8PwC7ZCMnSa1tAQclbRJpcNP1zQIBnB1to6etQ2wb8wiVcETDvT5dRZAUiNiZBFvt1RYgo5xUe1do4yeY5DxKIjjDBbElI8zZBBlz5yHRwigIcNYtyhHY9ZAtHL16ZAMPyo43wZANhGWE6o6EV7hXZBtxBEN24xTlGa7M
                                    [picture] => http://imgjy.com/FPB470/m/5236e26acc73a.jpg
                                    [link] => http://www.persunmall.com/persun/giveaway02.html?refer_id=1900
                                    [name] => my all_prams link <name>7458
                                )

                        )

                )

            [2] => Array
                (
                    [file] => D:\phpnow\htdocs\laji\fb_post.php
                    [line] => 98
                    [function] => api
                    [class] => BaseFacebook
                    [type] => ->
                    [args] => Array
                        (
                            [0] => Array
                                (
                                    [0] => Facebook Object
                                        (
                                            [sharedSessionID:protected] => 
                                            [appId:protected] => 176847305842335
                                            [appSecret:protected] => f79311ddff5888a165d46159b06f4670
                                            [user:protected] => 
                                            [signedRequest:protected] => 
                                            [state:protected] => 
                                            [accessToken:protected] => 
                                            [fileUploadSupport:protected] => 
                                            [trustForwarded:protected] => 
                                        )

                                    [1] => _graph
                                )

                            [1] => Array
                                (
                                    [0] => /me/feed
                                    [1] => POST
                                    [2] => Array
                                        (
                                            [access_token] => 1CAACg13jYHp8BAP0QEOK8PwC7ZCMnSa1tAQclbRJpcNP1zQIBnB1to6etQ2wb8wiVcETDvT5dRZAUiNiZBFvt1RYgo5xUe1do4yeY5DxKIjjDBbElI8zZBBlz5yHRwigIcNYtyhHY9ZAtHL16ZAMPyo43wZANhGWE6o6EV7hXZBtxBEN24xTlGa7M
                                            [picture] => http://imgjy.com/FPB470/m/5236e26acc73a.jpg
                                            [link] => http://www.persunmall.com/persun/giveaway02.html?refer_id=1900
                                            [name] => my all_prams link <name>7458
                                        )

                                )

                        )

                )

        )

)

3 个答案:

答案 0 :(得分:0)

没有尝试过,这不会起作用吗?

echo "Code: " . $e["error"]["code"];

答案 1 :(得分:0)

我在您的查询中理解的是您想要代码ID,即 [code] =&gt; 190 看到这里我们正在使用 FacebookApiException 类 有以下方法

**[0] => __construct
[1] => getResult
[2] => getType
[3] => __toString
[4] => getMessage
[5] => getCode
[6] => getFile
[7] => getLine
[8] => getTrace
[9] => getPrevious
[10] => getTraceAsString**

so from your code  

try{
$facebook->setAccessToken($access_token);
$rs= $facebook->api('/me/feed','POST',$post_data);
} catch (FacebookApiException $e) {
    print_r($e);
}

as per your code 


you can do two thing

1. 

       try{
            $facebook->setAccessToken($access_token);
            $rs= $facebook->api('/me/feed','POST',$post_data);
        } catch (FacebookApiException $e) {

           echo $e->getCode(); //which is the 5th method of the class

        }

or you can simply get all response like 

2. 


    try{
            $facebook->setAccessToken($access_token);
            $rs= $facebook->api('/me/feed','POST',$post_data);
        } catch (FacebookApiException $e) {

           $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'];

        }

希望这会对你有所帮助

答案 2 :(得分:0)

根据facebook doc。我不确定这是否有帮助。

$result = $e->getResult();
error_log(json_encode($result));

请关注:https://developers.facebook.com/docs/reference/php/exception-getResult