【问题标题】:posting in group on behalf of user using facebook graph API使用 Facebook 图形 API 代表用户在群组中发帖
【发布时间】:2014-06-04 06:50:30
【问题描述】:

我正在尝试代表用户发帖。我使用了此页面上给出的教程:http://25labs.com/updated-post-to-multiple-facebook-pages-or-groups-efficiently-v2-0/

我可以成功执行身份验证,但无法代表发帖。

这里是源代码:https://github.com/karimkhanp/fbPostOnBehalf

可以在这里进行测试:http://ec2-54-186-110-98.us-west-2.compute.amazonaws.com/fb/

有人经历过吗?

【问题讨论】:

    标签: php facebook facebook-graph-api


    【解决方案1】:

    我不熟悉本教程使用的批处理过程,但下面是发布到 Facebook 群组的代码示例

    <?php 
    # same this file as
    #     test.php
    
    include_once "src/facebook.php";
    $config = array(
        'appId' => "YOURAPPID",
        'secret' => "YOURAPPSECRET",
        'allowSignedRequest' => false, // optional, but should be set to false for non-canvas apps
    );
    
    class PostToFacebook 
    {
        private $facebook;
        private $pages;
    
    
        public function initialise($config){
            $this->name = "Facebook";
    
            // current necessary configs to set
            // $config = array(
            //  'appId' => FB_APP_ID,
            //  'secret' => FB_APP_SECRET,
            //  'allowSignedRequest' => false, // optional, but should be set to false for non-canvas apps
            // );
    
            $this->facebook = new Facebook($config);
            try{
                // if user removes app authorization
                $this->hasAccess = $this->has_permissions();
                if($this->hasAccess){
                    $this->groups = $this->getGroupData();
                }
            }
            catch(Exception $err){
    
            }
        }
        public function postMessageToGroup($message, $groupid){
            $messageResponse = array(
                'STATUS' => 0
            );
    
            $fbMessageObj = array(
                    "message" => strip_tags($message), 
            );
            try
            {
                $user_page_post = $this->facebook->api("/$groupid/feed", 'POST', $fbMessageObj);
                if($user_page_post && !empty($user_page_post['id'])){
                    $messageResponse['STATUS'] = 200;
                    $messageData = array(
                        'id' => $user_page_post['id'],
                        'link' => 'http://facebook.com/' . $user_page_post['id'],
                    );
                    $messageResponse['data'] = $messageData;                    
                }
                else{
                    $messageResponse['STATUS'] = 302;
                }
            }
            catch(Exception $err){
                $messageResponse['STATUS'] = 500;
                $messageResponse['data'] = array($err);
            }
    
            return $messageResponse;
        }
    
        // TODO: should read a template somewhere
        function show_login() {
            $login_url = $this->facebook->getLoginUrl( array( 'scope' => implode(",",$this->permissions()) ));
            return '<a href="' . $login_url . '">Login to Facebook and Grant Necessary Permissions</a>';
        }
        // TODO: should read a template somewhere
        public function toString()
        {
            if($this->hasAccess){
                if($this->groups){
                    $msg = "";
                    $msg .= '<select name="group_id"><option value=""></option>';
                    foreach($this->groups as $group) {
                        $msg .= '<option value="' .
                                  '' . urlencode($group['id']) .
                                  '">' .
                                  $group['name'] .
                                  '</option>' .
                                  '';
                    }
                    $msg .= '</select>';
                    return $msg;
                }
                else
                    return "No Groups";
            }
            else{
                return $this->show_login();
            }
        }
    
        function getGroupData(){
            $raw = $this->facebook->api('/me/groups', 'GET');
            $data = array();
            if (null != $raw && array_key_exists('data', $raw)) 
                return $raw['data'];
            return null;
        }
    
        // check if current instance has access to facebook
        function has_permissions() {
            $user_id = @$this->facebook->getUser();
            #print_r($user_id);
            if($user_id == null) return false;
            $permissions = $this->facebook->api("/me/permissions");
            foreach($this->permissions() as $perm){
                if( !array_key_exists($perm, $permissions['data'][0]) ) {   
                    return false;
                }
            }
            return true;
        }
    
        // permissins needed to post
        function permissions(){
            return array('manage_pages', 'user_groups');
        }
    }
    
    
    
    $fb = new PostToFacebook();
    $fb->initialise($config);
    if(!$fb->has_permissions())
    {
        echo $fb->show_login();
    }
    else{
        ?>
        <form method="post" action="test.php">
        <textarea name='message'></textarea>
        <?php echo $fb->toString(); ?>
        <input type='submit'>
        </form>
        <?php
    }
    if(!empty($_POST)){
        $response = $fb->postMessageToGroup($_POST['message'], $_POST['group_id']);
        if($response['STATUS'] == 200)
            print_r("<a href='" . $response['data']['link'] . "'>" . $response['data']['id'] ."</a>");
        else
        {
            echo "ERROR!";
            print_r($response);
        }
    }
    ?>
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-09
    • 2021-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多