使用PHP在Fanpage上创建事件

时间:2011-08-27 21:28:09

标签: php facebook facebook-graph-api

我需要在我的粉丝页面上创建活动。但目前这些事件显示在我的应用程序页面上而不是我的粉丝页面上。

你能告诉我一些如何做到这一点的信息吗?

2 个答案:

答案 0 :(得分:0)

您需要申请manage_pages权限才能访问“page”令牌。您可以使用您的应用令牌并转到

来获取该令牌
https://graph.facebook.com/userId/accounts?access_token={appToken} 

然后,除了传递包含其余事件数据的页面ID的所有者字段外,您还可以使用此功能。

这应该为页面创建事件。

答案 1 :(得分:0)

请参阅此链接http://pastebin.com/WSHCDLdr

这可能对你有帮助.........

此链接的PHP代码如下所示........

<?php

require_once 'fb.php';

$pageId = "XXXXXXXXXXXXXXXXXXX";


define('API_SECRET', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');

$baseurl = "http://example.com";

$facebook = new Facebook(array(
    'appId'  => 'XXXXXXXXXXXXXXXXXX',
    'secret' => API_SECRET,
    'cookie' => true,
    'fileUpload' => true
));

$session = $facebook->getSession();

$me = null;

// Session based API call.
if ($session) {
    $uid = $facebook->getUser();
//  $me = $facebook->api('/me');
    $me = $facebook->api("/$pageId");
}

if ($me) {
    $logoutMe = $facebook->getLogoutUrl(array('next' => $base_url));
} else {
    $loginMe =  $loginUrl = $facebook->getLoginUrl(array(
        'display'   => 'popup',
        'next'      => $baseurl /*. '?loginsucc=1'*/,
//      'cancel_url'=> $baseurl . '?cancel=1',
        'req_perms' => 'create_event'
    ));
}

// if user click cancel in the popup window
if ($me && empty($_GET['session']) && empty($_COOKIE['fbs_' . API_SECRET])) {
    die("<script>window.close();</script>");
} elseif($me && !empty($_GET['session'])) {
    //only if valid session found and loginsucc is set, 
    //after facebook redirects it will send a session parameter as a json value
    //now decode them, make them array and sort based on keys
    $sortArray = get_object_vars(json_decode($_GET['session']));
    ksort($sortArray);
    $strCookie  =   "";
    $flag       =   false;
    foreach($sortArray as $key=>$item){
        if ($flag) $strCookie .= '&';
        $strCookie .= $key . '=' . $item;
        $flag = true;
    }

    //now set the cookie so that next time user don't need to click login again
    setCookie('fbs_' . API_SECRET, $strCookie);

    die("<script>window.close();window.opener.location.reload();</script>");
}

if ($me) {
    var_dump($me);
    //Path to photo (only tested with relative path to same directory)
//  $file = "end300.jpg";
    //The event information array (timestamps are "Facebook time"...)
    $time = time() + rand(1, 100) * rand(24, 64) * 3600;
    $event_info = array(
        "privacy_type" => "SECRET",
//      "name" => "Event Title "  . time(),
        "name" => "Test Event Title "  . time(),
        "host" => "Me ",
        "start_time" => $time,
        "end_time" => $time + 120,
        "location" => "London " . time(),
        "description" => "Event Description " . time()
    );
    //The key part - The path to the file with the CURL syntax
//  $event_info[basename($file)] = '@' . realpath($file);
    //Make the call - returns the event ID
//  var_dump($facebook->api('me/events','post',$event_info));
    var_dump($facebook->api("$pageId/events", 'post', $event_info));
?>
<a href="<?= $facebook->getLogoutUrl() ?>">Logout</a>
<?
} else { ?>
<script type="text/javascript">
    var newwindow;
    var intId;
    function login(){
        var  screenX    = typeof window.screenX != 'undefined' ? window.screenX : window.screenLeft,
            screenY    = typeof window.screenY != 'undefined' ? window.screenY : window.screenTop,
            outerWidth = typeof window.outerWidth != 'undefined' ? window.outerWidth : document.body.clientWidth,
            outerHeight = typeof window.outerHeight != 'undefined' ? window.outerHeight : (document.body.clientHeight - 22),
            width    = 500,
            height   = 270,
            left     = parseInt(screenX + ((outerWidth - width) / 2), 10),
            top      = parseInt(screenY + ((outerHeight - height) / 2.5), 10),
            features = (
            'width=' + width +
            ',height=' + height +
            ',left=' + left +
            ',top=' + top
            );
        newwindow=window.open('<?=$loginUrl?>','Login by facebook',features);
        if (window.focus) {newwindow.focus()}
        return false;
    }
</script>
Please login to Facebook and we will setup the event for you! <br />
<a href="#" onclick="login();return false;">
    <img src="http://static.ak.fbcdn.net/rsrc.php/zB6N8/hash/4li2k73z.gif" border="0">
</a>
<?php } ?>