我想使用Facebook的PHP SDK发布Facebook用户的时间表。 postFacebook()方法包含发布到用户时间轴的代码。我的代码适用于Facebook登录,但Facebook发布不起作用。
将为用户填写的邮件设置自定义编辑器。来自用户的该消息将发布到用户的时间线上。我已经批准了我的Facebook应用程序,以获取访问令牌的“publish_actions”权限。
FacebookSession::setDefaultApplication( 'xxx', 'yyy' );
$helper = new FacebookRedirectLoginHelper( 'url' );
if ( isset( $_SESSION ) && isset( $_SESSION['fb_token'] ) ) {
$session = new FacebookSession( $_SESSION['fb_token'] );
try {
if ( !$session->validate() ) {
$session = null;
}
} catch ( Exception $e ) {
$session = null;
}
}
if ( !isset( $session ) || $session === null ) {
try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
print_r( $ex );
} catch( Exception $ex ) {
print_r( $ex );
}
}
if ( isset( $session ) ) {
$_SESSION['fb_token'] = $session->getToken();
$session = new FacebookSession( $session->getToken() );
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
$graphObject = $response->getGraphObject()->asArray();
postFacebook();
echo '<pre>' . print_r( $graphObject, 1 ) . '</pre>';
echo '<a href="' . $helper->getLogoutUrl( $session, 'url' ) . '">Logout</a>';
} else {
$loginUrl = $helper->getLoginUrl(array('scope' => 'email,publish_actions'));
header("Location: " . $loginUrl);
}
function postFacebook(){
$session = new FacebookSession($_SESSION['fb_token']);
try {
if(isset($_SESSION['event_website'])){
$request = new FacebookRequest(
$session,
'POST',
'/me/feed',
array (
'message' => $_SESSION['event_message'],
'link' => "http://www." . $_SESSION['event_website'] . ".xxx.com"
)
);
} else {
$request = new FacebookRequest(
$session,
'POST',
'/me/feed',
array (
'message' => $_SESSION['event_message']
)
);
}
} catch(FacebookRequestException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$response = $request->execute();
$graphObject = $response->getGraphObject();
}
?>