开发者

Create Events on Fanpage with PHP

I need to create events on my fanpage. But currently the events are shown on my application page not on my fanpage.

Can you give me some information on how I can do this开发者_运维问答?


You need to request the manage_pages permission which will give you access to "page" token. You can get that token by using your app token and going to

https://graph.facebook.com/userId/accounts?access_token={appToken} 

You can then use this in addition to passing in an owner field with the pages id with the rest of your event data.

This should create the event for the page.


see this link http://pastebin.com/WSHCDLdr

this may help you.........

PHP CODE AT THIS LINK IS GIVEN BELOW........

<?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 } ?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜