当用户点击Facebook应用程序中的按钮时,发布PHP GD处理的图像或其他内容


posting a php GD processed image or something else when user clicks a button in facebook application

当用户点击按钮时,如何在用户墙上发布一些东西(可能是PHP处理的图像或文本)。例如,假设Facebook用户安装了我的应用程序,然后它显示了一些经过处理的图像,然后如果用户希望发布,他单击发布按钮并发布到他的墙上,否则它不会。我面临的困难是从一个页面转到另一个页面(从索引.php到上传.php),转移access_token,以及如何进行 api 调用以上传图像.php我是否应该上传.php作为索引.php以便它可以重新验证用户?

你听起来真的很困惑。

如果您将文件命名为 index.phpupload.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>