使用facebook图形API代表用户发布组 [英] posting in group on behalf of user using facebook graph API

查看:126
本文介绍了使用facebook图形API代表用户发布组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以用户身份发帖。我已经使用了这个页面上给出的教程: http://25labs.com/updated-post-to-multiple-facebook-pages-or-groups-efficiently-v2-0/

I am trying to post on behalf of user. I have used tutorial given on this page: http://25labs.com/updated-post-to-multiple-facebook-pages-or-groups-efficiently-v2-0/ .

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

I could successfully perform authentication but could not post on behalf.

以下是源代码: https:// github。 com / karimkhanp / fbPostOnBehalf

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

Testing can be done here: http://ec2-54-186-110-98.us-west-2.compute.amazonaws.com/fb/

有没有人经历过这个?

推荐答案

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

I'm not familiar with the batch process that the tutorial is using but below is a code sample that posts to a Facebook group

<?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);
    }
}
?>

这篇关于使用facebook图形API代表用户发布组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆