在Facebook上发布新闻

时间:2011-03-19 17:11:19

标签: php facebook api

$config = array(
 'appId'  => 'yyyyyyyyyy',
 'secret' => 'xxxxxxxxxx',
 'cookie' => true,
 'domain' => true
);

$facebook_client = new Facebook($config);

//Grab the user's session
$session = $facebook_client->getSession();

/*If session does not exist, the user is not loggedin or hasn't added the app
so redirect them to the authorize page.*/
if(!$session){
 $text = "<script type=\"text/javascript\">\ntop.location.href = \"$oauth_url\";\n</script>";
 echo $text;
 exit;
}
$access_token = $access['access_token'];
$params = array('access_token' => $access_token);
try{
$me = $facebook_client->api('/me',$params);
$feed_params = array();
$feed_params['message'] = "Hello world";
$feed_params['link'] = "http://apps.facebook.com/jagdish/";
$feed_params['name'] = "jag";
$feed_params['caption'] = "Trying to post from application";
$feed_params['description'] = "From jag";
$feed_params['access_token'] = $access_token;
$id = me['id'];
$result = $facebook->api('/me/feed/','post',$feed_params);
}
catch(FacebookApiException $e)
{
    error_log($e);
}

这是我的代码。当我执行它时,有一个例外说

com.caucho.quercus.QuercusException: com.caucho.quercus.QuercusErrorException: /base/data/home/apps/fbookworkshop/version1.349114876107177725/index.php:49: Fatal Error: Method call 'api' is not allowed for a null value.

我哪里错了?

2 个答案:

答案 0 :(得分:1)

我猜你们正在混合3个(或更多)教程!

您的代码中的问题:
第一期:

$text = "<script type=\"text/javascript\">\ntop.location.href = \"$oauth_url\";\n</script>";

在这一行:

  1. 您在哪里设置$oauth_url
  2. 也不需要\n
  3. 第二个问题(由@fazo提及)。

    如果有效会话,

    access_token的第三个问题 NO NEED 因此,只要您使用/me,就不要在任何请求中设置或使用它。

    第四个问题:

    $result = $facebook->api('/me/feed/','post',$feed_params);
    

    您正在使用其他教程中的代码,因为您使用的是$facebook var而不是$facebook_client

答案 1 :(得分:0)

变化

$access_token = $access['access_token'];

$access_token = $session['access_token'];

并告诉我们发生了什么

<强>更新

看起来你不能用/我

https://github.com/facebook/php-sdk/issues/closed#issue/294