Facebook用户身份验证和用户墙上的帖子

时间:2012-03-15 18:03:21

标签: php facebook facebook-graph-api

我关注了this视频教程,但我陷入了困境。当我点击登录链接时,浏览器会继续加载,然后没有任何反应,仍然登录页面在我面前。它始终在$ user_id中返回0它之前向我展示了facebook身份验证页面1到2次,我点击了“允许”但现在没有发生任何事情。更多的事情,当我点击登录链接时,浏览器网址会改变

“http://localhost/php/fbdata.php”

“HTTP://localhost/php/fbdata.php状态= e1d5ccf919c6a9d3325200596c12b447&安培;代码= AQDg8hE1GuKQ6eq9nGeLV8BIK8EjeIuY8Drf6g0FwAWAjGkvFi70EoNquPmoYsk2PxKpcfxVWYqNgVxU7rRQ-xBxcZXziH5n9IXNNl1KKzLtUYgVRKqWRczh2wINcvDY8WuWoMpIETxWpYhIbrZ-w46xB1v2YMADbOfrFNxLhiyIC239GIQRC__Tw_KiYoZiK1A# =

我不知道它是否会给我一些东西或其他东西。 我想要的是我可以在我的脸书页面发布“你好世界”。这是我的PHP代码。

require_once('facebook.php');


$config = array(
'appId' => 'xxxxx',
'secret' => 'xxxxxxxxxx',
'cookie' => 'true'
  );

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

$user_profile=null;  

if($user_id) 
{

  try 
  {
        $user_profile = $facebook->api('/me');
        $facebook->api('/me/feed','post',array(
        'message'=>'hello world'
        ));

  } 
  catch(FacebookApiException $e) 
  {
        echo $e->getMessage();
  }   

}

if($user_id)
{
    $logout_url=$facebook->getLogoutUrl();  
    echo "<a href='$logout_url'>Logout</a>";
}
else
{
    $login_url=$facebook->getLoginUrl(array(
    'scope'=>'read_friendlists,publish_stream,email,user_work_history,user_website,user_religion_politics,user_relationship_details,user_relationships,user_interests,user_hometown,user_education_history,user_birthday,user_about_me'
    ));
    echo "<a href='$login_url'>Login</a>";

}

1 个答案:

答案 0 :(得分:0)

Mauzzam,

此代码适用于我 - 请查看PHP SDK

中的examples文件夹
<?php
require '../src/facebook.php';

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId'  => '350881691595504',
  'secret' => 'a50ae255f0ba3c24000e38ede7f666b9',
));

$user = $facebook->getUser();

if ($user) {
  try {
    $response = $facebook->api('/me/feed','post',array(
      'message'=>'hello world'
    ));
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}

// Login or logout url will be needed depending on current user state.
if ($user) {
  $logoutUrl = $facebook->getLogoutUrl();
} else {
  $loginUrl = $facebook->getLoginUrl();
}

?>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
  <head>
    <title>PHP SDK TEST</title>
  </head>
  <body>
    <?php if ($user): ?>
      <a href="<?php echo $logoutUrl; ?>">Logout</a>
    <?php else: ?>
      <div>
        Login using OAuth 2.0 handled by the PHP SDK:
        <a href="<?php echo $loginUrl; ?>">Login with Facebook</a>
      </div>
    <?php endif ?>

    <?php if ($user): ?>
      <h3>Hello World Result</h3>
      <pre><?php print_r($response); ?></pre>
    <?php else: ?>
      <strong><em>You are not Connected.</em></strong>
    <?php endif ?>
  </body>
</html>