使用php的Ajax facebook帖子


Ajax facebook post with php

好的,所以我正在尝试使用ajax发布到facebook。问题是我没有登录。这是正在请求的页面。config只包含我的facebook应用程序身份验证详细信息。我想我只是不明白需要做什么——当我尝试用ajax做这个请求时,我尝试过的所有东西都返回了0(没有用户)。

<?php
require_once 'includes/config.php';
//Including facebook php sdk file
require_once 'includes/php-sdk/facebook.php';
//Creating our application instance
$facebook = new Facebook(array(
            'appId' => APP_ID,
            'secret' => APP_SECRET,
            'cookie' => true
        ));
//Get User ID
$user = $facebook->getUser();
if ($user) {
    try {
        // Proceed knowing you have a logged in user who's authenticated.
        $user_profile = $facebook->api('/me');
        $access_token = $facebook->getAccessToken(); 
    } catch (FacebookApiException $e) {
        error_log($e);
        $user = null;
    }
    //In order to post to the page later on we need to generate an Access Token for that page, to do this we get me-accounts in the following api call
    $accounts = $facebook->api('/me/accounts');
}

看看https://github.com/facebook/facebook-php-sdk/blob/master/examples/example.php

上面写着

$facebook = new Facebook(array(
  'appId'  => '344617158898614',
  'secret' => '6dc8ac871858b34798bc2488200e503d',
));
// Get User ID
$user = $facebook->getUser();
// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.
if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
  } 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();
}

尝试此代码,可能您从未使用API的facebook帐户登录(在本例中,我指的是代码的getLogoutUrl()和getLoginUrl()位),在这种情况下,您需要将用户引用到$loginUrl,例如:

header("location: $loginUrl");