如何授权我的PHP代码在Facebook上发布?

时间:2012-09-17 13:54:01

标签: php facebook facebook-graph-api

我想在我的Facebook USER帐户墙上发布一些测试墙帖(不是任何Facebook页面或Facebook应用程序只是普通用户帐户)。我试过像下面的Facebook SDK,但没有发生任何事只是让我登录。但我想以编程方式登录,请告诉我如何做到这一点并张贴到我自己的墙上?

require_once "API_Library/Facebook/src/facebook.php";
$config = array(
         'userId' => 'USER_ID',
     );

     $facebook = new Facebook($config);
     $user_id = $facebook->getUser(); 

if($user_id)
             {

                 // We have a user ID, so probably a logged in user.
                 // If not, we'll get an exception, which we handle below.
                 try
                 {
                     $user = $config['userId'];
                     $user_profile = $facebook->api('/me', 'GET');
                     echo "Name: " . $user_profile['name'];
                     $ret_obj = $facebook->api('/me/feed', 'GET', array(
                         'link' => "https://www.facebook.com/$user",
                         'message' => 'Posting with the PHP SDK!'
                             ));
                     echo '<pre>Post ID: ' . $ret_obj['id'] . '</pre>';
                 }
                 catch(FacebookApiException $e)
                 {
                     $login_url = $facebook->getLoginUrl();
                     echo 'Please <a href="' . $login_url . '">login.</a>';
                     error_log($e->getType());
                     error_log($e->getMessage());
                 }
             }
             else
             {

                 // No user, print a link for the user to login
                 $login_url = $facebook->getLoginUrl();
                 echo 'Please <a href="' . $login_url . '">login.</a>';
             }

1 个答案:

答案 0 :(得分:1)

您正在

进行HTTP GET而不是HTTP POST
$ret_obj = $facebook->api('/me/feed', 'GET', array(
                     'link' => "https://www.facebook.com/$user",
                     'message' => 'Posting with the PHP SDK!'
                         ));

您可能希望将API方法更改为POST