簡體   English   中英

使用PHP 2011在用戶的Facebook牆上發布

[英]Posting on user's facebook wall using php 2011

$ result = $ facebook-> api('/ me / feed /','post',$ attachment)之后,一切都不會擱在牆上,執行會變得毫無嘗試。 聲明,任何想法都壞了。

$facebook = new Facebook(array(
  'appId'  => 'xxxxxxxxxxxx',
  'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
));

// Get User ID


$user = $facebook->getUser();
if ($user) {
  try {
    // Get the user profile data you have permission to view
    $user_profile = $facebook->api('/me');
    $uid = $facebook->getUser();


      $url = $facebook->getLoginUrl(array(
    'canvas' => 1,
    'fbconnect' => 0,
    'req_perms' => 'email,publish_stream,status_update,user_birthday,user_location,user_work_history'));


    $attachment = array
 (
 'access_token'=>$facebook->getAccessToken(),
 'message' => 'I had a question: should I write a PHP facebook app that actually worked?',
 'name' => 'I Asked Bert',
 'caption' => 'Bert replied:',
 'link' => 'http://apps.facebook.com/askbert/',
 'description' => 'NO',
 'picture' => 'http://www.facebookanswers.co.uk/img/misc/question.jpg'
 );
 echo "Test 1"; 

$result = $facebook->api('/me/feed/','post',$attachment);

    echo "Test 2"; 
    $_SESSION['userID'] = $uid;


  } catch (FacebookApiException $e) {
    $user = null;
  }
} else {
  die('Somethign Strange just happened <script>top.location.href="'.$facebook->getLoginUrl().'";</script>');
}

測試1已打印,但測試2未打印。

您說您正在尋找更新的文檔,是否檢查了Facebook PHP-SDK常見問題解答

特別,

  • 如何授權並擁有以下任何權限?
  • 如何在牆上張貼?

您創建一個應用程序實例后讓你的$user 第一

$user = $facebook->getUser();

從此處開始,按照“如何授權並具有以下任何權限?”中的說明進行操作。 使用范圍

$par = array();
$par['scope'] = "publish_stream";

檢查用戶狀態,以查看通過publish_stream權限所需的登錄/注銷方法

if ($user) {
        $logoutUrl = $facebook->getLogoutUrl();
} else {
        $loginUrl = $facebook->getLoginUrl($par);
}

然后按照“如何在牆上張貼?”中的說明放置附件。

if ($user) {
$attachment = array('message' => 'this is my message',
    'name' => 'This is my demo Facebook application!',
    'caption' => "Caption of the Post",
    'link' => 'http://mylink.com/ ',
    'description' => 'this is a description',
    'picture' => 'http://mysite.com/pic.gif ',
    'actions' => array(array('name' => 'Get Search',
    'link' => 'http://www.google.com/ '))
    );

    try {
    // Proceed knowing you have a user who is logged in and authenticated
    $result = $facebook->api('/me/feed/','post',$attachment);
    } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
    }

}

示例應用程序中所述,放入try / catch塊以查看可用數據,具體取決於在進行API調用時用戶是否已登錄。

諸如

$cocacola = $facebook->api('/cocacola');

由於它是公開可用的,因此將始終有效。

以下是一些注意事項:

  1. 您正在使用新的PHP-SDK,所以不要使用req_perms使用scope
  2. 將您的/me/feed帖子通話放入 try
  3. 您不需要調用getUser()兩次,用戶ID已在$user
  4. 用戶ID將已經在會話中,其密鑰如下所示: fb_XXXXXXX_user_id其中XXXXXXX是您的應用ID
  5. 會話已經開始。

下面的代碼肯定有效:即使您沒有權限,它也會嘗試獲取它們

$facebook = new Facebook(array(
  'appId'  => 'xxxxxxxxxxxxxxxxxx',
  'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
));
$user = $facebook->getUser();
$user_profile = $facebook->api('/me');


if( array_key_exists('publish_stream', $permissions['data'][0]) ) {
                // Permission is granted!
                // Do the related task
                //$post_id = $facebook->api('/me/feed', 'post', array('message'=>'Hello World!'));
                  $post_id = $facebook->api('/me/feed', 'post', $attachment);

            } else {
                // We don't have the permission
                // Alert the user or ask for the permission!
                echo "Click Below to Enter!";
                header( "Location: " . $facebook->getLoginUrl(array("scope" => "publish_stream")) );
            }

*警告

截至2011年9月5日,此功能已開始運行,但我在fb文檔中看到,他們正在更改在用戶牆上發布的方法,並且不鼓勵使用發布流。 但目前

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM