需要帮助在用户墙上发布

时间:2011-02-26 11:38:44

标签: php facebook

需要帮助下面是我的应用程序的index.php的代码,我想在用户使用OFFLINE_ACCESS和publish_stram

授权我的应用程序后在用户墙上发布
<?php 

 $app_id = "XXXXXXXXXX";

 $app_secret = "XXXXXXX";

 $canvas_page = "http://apps.facebook.com/esccounsel/";

 $auth_url = "http://www.facebook.com/dialog/oauth?client_id=" 
        . $app_id . "&redirect_uri=" . urlencode($canvas_page) . ("&scope=read_stream publish_stream offline_access");

 $signed_request = $_REQUEST["signed_request"];

 list($encoded_sig, $payload) = explode('.', $signed_request, 2); 

 $data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);

 if (empty($data["user_id"])) {
        echo("<script> top.location.href='" . $auth_url . "'</script>");
 } else {
        echo ("Welcome User: " . $data["user_id"]);
 } 

?>

我想直接在用户墙上发帖请回答

1 个答案:

答案 0 :(得分:6)

  1. 下载Facebook PHP-SDK
  2. 无需offline_access权限
  3. 熟悉PHP-SDK并使用example页面中的代码作为索引,如:

    <?php
    require '../src/facebook.php';
    $facebook = new Facebook(array(
      'appId'  => 'XXXXXXX',
      'secret' => 'XXXXXXXX',
      'cookie' => true,
    ));
    
    $session = $facebook->getSession();
    $loginUrl = $facebook->getLoginUrl(array(
        "req_perms" => "publish_stream"
    ));
    $me = null;
    
    if ($session) {
      try {
        $uid = $facebook->getUser();
        $me = $facebook->api('/me');
    
        echo "Welcome User: " . $me['name'] . "<br />";
    
        $post_id = $facebook->api("/$uid/feed", "post", array("message"=>"Hello World!"));
        if(isset($post_id))
            echo "A new post to your wall has been posted with id: $post_id";
    
      } catch (FacebookApiException $e) {
        error_log($e);
      }
    } else {
        echo("<script> top.location.href='" . $loginUrl . "'</script>");
    }
    ?>
    
  4. 欢迎使用Stackoverflow!