4
以下代碼有效,但它以當前登錄的頁面管理員身份創建事件。我需要它將事件發佈到粉絲頁面本身。 FYI:我的應用程序已被授予的權限範圍:manage_pages
如何使用PHP SDK和Graph API將事件發佈到Facebook粉絲頁面?
<?php
// Initialize a Facebook instance from the PHP SDK
$config = array(
'appId' => FACEBOOK_APP_ID,
'secret' => FACEBOOK_APP_SECRET,
'cookie' => false,
'scope' => 'manage_pages,create_event'
);
$facebook = new Facebook($config);
$access_token = $facebook->getAccessToken();
$page_id = 'XXXXXXXXXXXX';
// Now, getting the PAGE Access token, using the user access token
$page_token_url = "https://graph.facebook.com/$page_id?fields=access_token&" . $access_token;
$response = file_get_contents($page_token_url);
// Parse the return value and get the Page access token
$resp_obj = json_decode($response,true);
$page_access_token = $resp_obj['access_token'];
// Declare the variables we'll use to demonstrate
// the new event-management APIs
$event_id = 0;
$event_name = "New Event API Test Event";
$event_start = time() + rand(1, 100) * rand(24, 64) * 3600;
$event_privacy = "SECRET"; // We'll make it secret so we don't annoy folks.
// We'll create an event in this example.
// We'll need create_event permission for this.
$params = array(
'name' => $event_name,
'start_time' => $event_start,
'privacy_type' => $event_privacy,
'access_token' => $page_access_token,
'page_id' => $page_id //where $page_id is the ID of the page you are managing
);
// Create an event
$ret_obj = $facebook->api("/$page_id/events", 'POST', $params);
if(isset($ret_obj['id'])) {
// Success
$event_id = $ret_obj['id'];
printMsg('Event ID: ' . $event_id);
} else {
printMsg("Couldn't create event.");
}
// Convenience method to print simple pre-formatted text.
function printMsg($msg) {
echo "<pre>$msg</pre>";
}
?>
我想通了:我添加了參數'page_id'到'$ params'的列表中,並給了它'$ page_id'的值,並且它馬上消失了!如果我能夠在接下來的幾個小時內回憶起由於信譽低下而能夠幫助其他人的情況,我會發布修改後的代碼。不管謝謝! – lincolnberryiii
這是上面修改的代碼嗎?我現在可以真正使用一些幫助! :) –
@JodyHeavener我剛剛爲你更新。 – lincolnberryiii