使用Graph API通过PHP发布到Facebook Stream

时间:2010-10-20 20:48:48

标签: php facebook

我正在尝试使用新的图API和PHP将消息发布到用户的墙上。连接似乎工作正常,但没有帖子出现。我不确定如何正确设置发布代码。请帮帮我。对于破旧的代码感到抱歉,由于某种原因,StackOverflow不想在代码块中关闭它。

以下是我的完整代码。我错过了扩展程序权限请求,或者在此代码中注意了这一点:

PHP代码

<?php

include_once 'facebook.php';

$facebook = new Facebook(array(
    'appId'  => 'xxxxxxxxxxxxxxxxxx',
    'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxx',
    'cookie' => true
));

$session = $facebook->getSession();

if (!$session) {
    $url = $facebook->getLoginUrl(array(
        'canvas' => 1,
        'fbconnect' => 0
    ));
    echo "<script type='text/javascript'>top.location.href = '$url';</script>";

} else {
    try {
        $uid = $facebook->getUser();
        $me = $facebook->api('/me');
        $updated = date("l, F j, Y", strtotime($me['updated_time']));
        echo "Hello " . $me['name'] . "<br />";
        echo "You last updated your profile on " . $updated;

        $connectUrl = $facebook->getUrl(
          'www',
          'login.php',
          array_merge(array(
            'api_key'         => $facebook->getAppId(),
            'cancel_url'      => 'http://www.test.com',
            'req_perms'       => 'publish_stream',
            'display'         => 'page',
            'fbconnect'       => 1,
            'next'            => 'http://www.test.com',
            'return_session'  => 1,
            'session_version' => 3,
            'v'               => '1.0',
          ), $params)
        );

        $result = $facebook->api(
            '/me/feed/',
            'post',
            array('access_token' => $facebook->access_token, 'message' => 'Playing around with FB Graph..')
        );

    } catch (FacebookApiException $e) {
        echo "Error:" . print_r($e, true);
    }
}
?>

1 个答案:

答案 0 :(得分:6)

这是因为您没有选择“req_perms”并允许该应用将流发布到您的墙上。确保在“req_perms。

中添加”publish_stream“

PHP代码:

$url = $facebook->getLoginUrl(array(
                   'canvas' => 1,   //set to 1 bcoz my application is Iframe app
                   'fbconnect' => 0,
                   'req_perms' => 'publish_stream'
               ));