Posting news feeds in facebook
$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'] = "h开发者_Python百科ttp://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);
}
This is my code. When i executed it, got an exception saying
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.
Where am i going wrong?
I guess you are mixing 3 (or more) tutorials together!!
Issues in your code:
The first issue:
$text = "<script type=\"text/javascript\">\ntop.location.href = \"$oauth_url\";\n</script>";
In this line:
- Where did you set the
$oauth_url
? - Also no need for the
\n
The second issue (mentioned by @fazo).
The third issue, NO NEED for the access_token
all together if there's a valid session! so don't set it or use it in any of your requests as long as you are using /me
.
The fourth issue:
$result = $facebook->api('/me/feed/','post',$feed_params);
Here you are using the code from another tutorial, since you are using $facebook
var instead of $facebook_client
change
$access_token = $access['access_token'];
to
$access_token = $session['access_token'];
and tell us what happened
UPDATE:
looks like you can't use /me
https://github.com/facebook/php-sdk/issues/closed#issue/294
精彩评论