2013-12-14 139 views
1

我是FB應用程序和FB頁面的管理員(但此頁面在單獨的帳戶中)。我如何使用FB API和PHP通過這個FB應用程序發佈一些東西到這個頁面的牆上(爲了能夠用CRON來完成)?那可能嗎?預先感謝您的答案!通過Facebook API發佈到頁面

回答

1

是的,這是可能的。

首先,使用頁面訪問令牌完成頁面上的發佈。

從您的應用程序獲取正常的令牌權限(可直接使用Graph API Explorer從在選擇應用右上角的下拉菜單):manage_pages,然後按照我在這裏提到的步驟:https://stackoverflow.com/a/18322405/1343690 - 這將讓你一個永不過期頁面訪問令牌。

將它保存在某處並在發佈時用於您的cron-job。張貼代碼 -

$url = 'https://graph.facebook.com/{page-id}/feed'; 
$attachment = array(
    'access_token' => $page_access_token, 
    'message' => '{your-message}' 
); 
$result = PostUsingCurl($url, $attachment); 
$result = json_decode($result, TRUE); 
if(isset($result['error'])) { 
    echo "Error: ".$result['error']['message']."<br/>"; 
} 
else{ 
    echo "Feed posted successfully!<br/>"; 
} 

function PostUsingCurl($url, $attachment) 
{ 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment); 
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    $result = curl_exec($ch); 
    curl_close ($ch); 

    return $result; 
} 
0

阿比添加到您的網頁

<script id="facebook-jssdk" src="//connect.facebook.net/en_US/all.js#xfbml=1"></script>` 

點擊功能調用的FB

$('#facebook').click(function(){ 
    FB.init({ 
    appId: 12345, // your app ID 
    status: true, 
    cookie: true 
    }); 
    FB.ui({ 
    method: 'feed', 
    name: "post name", 
    link: "http://postlink.com, 
    //picture: "http:/imageurl.com, 
    description: "this is the body of the text" 
    }); 

}) 
+0

感謝您的回答,但那不是我想要的。您的代碼有效,但它會在我的個人帳戶的牆上留下一條消息,而不是頁面的牆。另外,我忘了提及我想通過PHP來實現基於服務器的功能。我相應地更新了我的問題。 –

0

我個人使用。儘管您需要生成一個access_token。如果您不這樣做,則可以使用Facebook's Graph Explorer tool爲您的帳戶授予適當的權限。

$attachment = array(
"access_token" => $fb_token, 
"link" => "$postLink", 
"name" => "$postName", 
"description" => "$postDescription", 
"message" => "$postMessage", 
"fb:explicitly_shared" => true 
); 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/'.$fb_page_id.'/feed'); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //to suppress the curl output 
$result = curl_exec($ch); 
curl_close ($ch); 

希望這能幫上忙!

相關問題