2012-03-23 81 views
3

我有一個腳本可以發佈到Facebook,但我有問題的數據如何發佈在Facebook上。使用php發佈到Facebook頁面

因爲它可以幫助別人這裏是完整的代碼

<?php 
require 'facebook.php'; 
Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYHOST] = 0; 
Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = 0; 

$pageId = "XXXXXXXX"; 
$permSess = "XXXXXXXXXXXXXXXX"; 

$facebook = new Facebook(array(
    "appId" => "XXXXXXXXXX", 
    "secret" => "XXXXXXXXXX", 
    "cookie" => true 
)); 

$page = $facebook->api("/{$pageId}"); 
?> 

<!doctype html> 
<html xmlns:fb="http://www.facebook.com/2008/fbml"> 

    <head> 
     <title>Post to Page Wall</title> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" 
     /> 
     <meta name="description" content="Post to page's wall" /> 
     <style> 
      body { 
       font-family:'Lucida Grande', Verdana, Arial, sans-serif; 
       background-color: #f2f2f2; 
      } 
      h1 a { 
       text-decoration: none; 
       color: #3b5998; 
      } 
      h1 a:hover { 
       text-decoration: underline; 
      } 
      form { 
       border: 1px solid #eee; 
       padding: 20px; 
       width: 550px; 
      } 
      textarea, select, input, label { 
       width: 500px; 
       border: 1px solid #ddd; 
       height: 20px; 
       clear: both; 
       margin: 10px; 
      } 
      textarea { 
       height: 100px; 
      } 
      label { 
       border: none; 
       font-weight: bold; 
      } 
      input#submit { 
       width: 100px; 
      } 
     </style> 
    </head> 

    <body> 
     <h1>Post to Friend's Wall</h1> 
     <?php if(isset($_POST[ 'submit'])) { $link=$ 
     _POST[ 'link']; $message=$ _POST[ 'message']; $attachment=a rray($message,$link 
     ); $rest=$ facebook->api(array("uid" => $pageId, "method" => "stream.publish", "access_token" 
      => $permSess, "message" => $message)); } ?> 
      <form id="Wall" name="Wall" 
      method="post"> 
       <label for="URL">URL:</label> 
       <input id="link" name="link"> 
       <label for="Message">Message:</label> 
       <textarea id="message" name="message"></textarea> 
       <input type="submit" name="submit" id="submit" value="Send!"> 
      </form> 
    </body> 

</html> 

問題:

我用的是張貼here爲例,使用例如提交了一份關於在那裏得到張貼在URL字段中的鏈接Facebook就像你自己做的一樣(包括圖片和網站描述),但是使用我的代碼我不能以同樣的方式實現。

關於「How can I post to the wall of a Facebook Fan Page using PHP and Open Graph API」,有些用戶發佈了答案,但在我的情況下不起作用。

我的代碼,並在另一個網站上的代碼之間的區別是代碼

<?php 
if (isset($_POST['submit'])) { 
    $sendTo = $_POST['friend']; 
    $link = $_POST['link']; 
    $message = $_POST['message']; 

    // all options: https://stackoverflow.com/questions/691425/how-do-you-post-to-the-wall-on-a-facebook-page-not-profile 
    $attachment = array(
     'message' => $message, 
     'link' => $link 
    ); 

    if ($result = $facebook->api("/$sendTo/feed/", 'post', $attachment)) { 
     $feedbackMessage = "Message sent to friend $sendTo"; 
    } else { 
     $feedbackMessage = "Oops something went wrong"; 
    } 
} 
?> 

的「$附件」部分所以......問題是如何正確地張貼到Facebook頁面沒有損失使用我的腳本的任何好處或格式。

在此先感謝

+1

請將更改的代碼發佈在下面的答案中,而不是更改問題。 – BoltClock 2012-03-25 16:59:24

回答

3

,可以讓你發佈到Facebook頁面,並保持格式,我得到了你的意思!

<?php 
if(isset($_POST['submit'])) { 
      $link = $_POST['link']; 
      $message = $_POST['message']; 



$attachment = array(
     'access_token' => $permSess, 
     'message' => $message, 
     'link' => $link 

    ); 
if( $result = $facebook->api(
     "/".$pageId."/links", 
     'post', 
     $attachment 
    )){ 
       $feedbackMessage = "Message sent to friend $sendTo"; 
      } else { 
       $feedbackMessage = "Oops something went wrong"; 
      } 

} 
?>