使用php 2011在用户的Facebook墙上发布

PUG*_*PUG 0 php facebook facebook-graph-api

没有任何东西被贴到墙上,执行结束后试试$ 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>');
}
Run Code Online (Sandbox Code Playgroud)

打印测试1但不测试测试2.

phw*_*hwd 5

你说你在寻找更新的文档,你检查过Facebook PHP-SDK FAQ吗?

特别,

  • 如何授权并拥有以下任何权限?
  • 如何贴在墙上?

创建应用程序实例后,获取$user 第一个

$user = $facebook->getUser();
Run Code Online (Sandbox Code Playgroud)

从这里开始,按照"如何授权并拥有以下任何权限?"中的说明进行操作.使用范围

$par = array();
$par['scope'] = "publish_stream";
Run Code Online (Sandbox Code Playgroud)

检查用户状态以查看通过publish_stream权限所需的登录/注销方法

if ($user) {
        $logoutUrl = $facebook->getLogoutUrl();
} else {
        $loginUrl = $facebook->getLoginUrl($par);
}
Run Code Online (Sandbox Code Playgroud)

然后按照"如何在墙上张贴?"中的说明放置附件.

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;
    }

}
Run Code Online (Sandbox Code Playgroud)

示例应用程序中所述,在进行API调用时,根据用户是否登录,放入try/catch块以查看可用的数据.

像这样的电话

$cocacola = $facebook->api('/cocacola');
Run Code Online (Sandbox Code Playgroud)

因为它是公开的,所以总能工作.