只需检查当前的Facebook用户ID
$user,如果它返回null,则需要重新授权用户(或使用自定义
$_SESSION用户ID值-不推荐)
require 'facebook/src/facebook.php';// Create our Application instance (replace this with your appId and secret).$facebook = new Facebook(array( 'appId' => 'APP_ID', 'secret' => 'APP_SECRET',));$user = $facebook->getUser();$photo_details = array('message' => 'my place');$file='photos/my.jpg'; //Example image file$photo_details['image'] = '@' . realpath($file);if ($user) { try { // We have a valid FB session, so we can use 'me' $upload_photo = $facebook->api('/me/photos', 'post', $photo_details); } catch (FacebookApiException $e) { error_log($e); }}// login or logout url will be needed depending on current user state.if ($user) { $logoutUrl = $facebook->getLogoutUrl();} else {// redirect to Facebook login to get a fresh user access_token $loginUrl = $facebook->getLoginUrl(); header('Location: ' . $loginUrl);}


