开发者

Facebook API (Via PHP): Post to Feed?

开发者 https://www.devze.com 2023-03-07 05:45 出处:网络
I\'ve got the basic idea covered (I think开发者_如何学C), but can\'t seem to successfully post anything using Facbook\'s provided PHP class. Given this example, can anyone see anything that might be t

I've got the basic idea covered (I think开发者_如何学C), but can't seem to successfully post anything using Facbook's provided PHP class. Given this example, can anyone see anything that might be the problem?

// Initialize Facebook API
$fb = new Facebook(array(
    'appId' => FACEBOOK_APP_ID,
    'secret' => FACEBOOK_SECRET,
    'cookie' => true
));

if($fb->getSession()) {
    $res = $fb->api('/me/feed/', 'post', array(
        'message' => 'test message',
        'description' => 'testing'
    ));
}

var_dump($res);

When I output the contents of $res I get a face-palming NULL. Any ideas? If the above looks correct, can anyone suggest some things I might try to debug this?


In your app, when you check to see if the user is logged into Facebook, make sure you are confirming that your app requested the 'special privilege' of posting to the user's wall. You can do this by modifying your code as follows:

$url = $this->facebook->getLoginUrl(array('canvas' => 1, 'fbconnect' => 0, 'req_perms' => 'user_status,publish_stream'));

Note the req_perms key being set to a list of values including 'publish_stream'. If you don't have this privilege the feed call will fail.

Also whether this is really the problem or not, you can wrap your Facebook requests with a try-catch block to catch FacebookApiException type exceptions to get more information when you have errors. For example:

        try {
            $uid = $this->facebook->getUser();
            $me = $this->facebook->api('/me');
        } catch (FacebookApiException $e) {
            print_r($e);
        }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号