当用户点击一个按钮时,如何在用户墙上发布一些东西(可能是一个 php 处理的图像或一些文本)。例如,假设一个 facebook 用户安装了我的应用程序,然后它会显示一些处理过的图像,然后如果用户希望发布,他单击发布按钮并发布到他的墙上,否则不会。我面临的困难是从一个页面转到另一个页面(如从 index.php 到 upload.php),传输access_token,以及如何进行 api 调用以从 upload.php 上传图像,如果我必须进行上传。 php 作为 index.php 以便它可以重新验证用户?
问问题
165 次
1 回答
0
你听起来真的很困惑。
如果您将文件命名index.php
为upload.php
.
我不明白为什么您需要手动处理访问令牌。使用 PHP SDK,它会自动为您完成。PHP SDK 使用该$_SESSION
变量来存储它。否则,您可以在 URL 中将其作为参数传递,例如:
upload.php?access_token=SOMETHING_REALLY_SECRET
不过我不建议这样做,因为它会使您的网站很容易受到攻击。
PHP SDK 文档中有一个完整的示例:
<?
// Remember to copy files from the SDK's src/ directory to a
// directory in your application on the server, such as php-sdk/
require_once('php-sdk/facebook.php');
$config = array(
'appId' => 'YOUR_APP_ID',
'secret' => 'YOUR_APP_SECRET',
'fileUpload' => true,
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
$photo = './mypic.png'; // Path to the photo on the local filesystem
$message = 'Photo upload via the PHP SDK!';
?>
<html>
<head></head>
<body>
<?
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>';
} 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());
}
echo '<br /><a href="' . $facebook->getLogoutUrl() . '">logout</a>';
} 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>
于 2012-06-24T18:22:29.233 回答