我是创建FB应用程序的新手,我从facebook上获取了这个上传图片代码来帮助我。它通过浏览器工作正常,但我不知道如何从命令行运行它而不会出错。有人可以帮我修改一下吗?看起来它无法登录Facebook。我已经查看了SO上的类似帖子,我理解访问令牌存在一些问题,但我一直对如何将检索访问令牌合并到以下代码中感到困惑:
<?php
// Remember to copy files from the SDK's src/ directory to a
// directory in your application on the server, such as php-sdk/
require ('./facebook_php_sdk/facebook.php');
$config = array(
'appId' => 'MYAPPID',
'secret' => 'MYSECRET',
'fileUpload' => true,
'allowSignedRequest' => false // optional but should be set to false for non-canvas apps
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
$photo = './myimage.jpg'; // Path to the photo on the local filesystem
$message = 'My image message';
?>
<html>
<head></head>
<body>
<?php
if($user_id) {
// We have a user ID, so probably a logged in user.
// If not, we'll get an exception, which we handle below.
try {
// Upload to a user's profile. The photo will be in the
// first album in the profile. You can also upload to
// a specific album by using /ALBUM_ID as the path
$ret_obj = $facebook->api('/me/photos', 'POST', array(
'source' => '@' . $photo,
'message' => $message,
)
);
echo '<pre>Photo ID: ' . $ret_obj['id'] . '</pre>';
echo '<br /><a href="' . $facebook->getLogoutUrl() . '">logout</a>';
} catch(FacebookApiException $e) {
// If the user is logged out, you can have a
// user ID even though the access token is invalid.
// In this case, we'll get an exception, so we'll
// just ask the user to login again here.
$login_url = $facebook->getLoginUrl( array(
'scope' => 'photo_upload'
));
echo 'Please <a href="' . $login_url . '">login.</a>';
error_log($e->getType());
error_log($e->getMessage());
}
} else {
// No user, print a link for the user to login
// To upload a photo to a user's wall, we need photo_upload permission
// We'll use the current URL as the redirect_uri, so we don't
// need to specify it here.
$login_url = $facebook->getLoginUrl( array( 'scope' => 'photo_upload') );
echo 'Please <a href="' . $login_url . '">login.</a>';
}
?>
</body>
</html>
编辑:从命令行收到错误消息:
<b>Warning</b>: session_start() [<a href='function.session-start'>function.session-start</a>]: Cannot send session cookie - headers already sent in <b>/home/user/public_html/site/modules/facebook_php_sdk/facebook.php</b> on line <b>59</b><br />
<br />
<b>Warning</b>: session_start() [<a href='function.session-start'>function.session-start</a>]: Cannot send session cache limiter - headers already sent (output started at /home/user/public_html/site/modules/facebook_php_sdk/facebook.php:59) in <b>/home/user/public_html/site/modules/facebook_php_sdk/facebook.php</b> on line <b>59</b><br />
<html>
<head></head>
<body>
Please <a href="https://www.facebook.com/dialog/oauth?client_id=524495667649391&redirect_uri=http%3A%2F%2F&state=804a8eebae4c3807118eeb7cac2b9f6f&sdk=php-sdk-3.2.3&scope=photo_upload">login.</a>