是否有可能从我的网站发布故事到Facebook页面墙而无需登录到Facebook?

时间:2012-09-17 12:40:01

标签: php facebook facebook-graph-api facebook-login

这是我用来从我的网站发布故事到Facebook页面墙的代码。我已经安装了像facebook.php和base_facebook.php等文件的facebook sdk。我的要求是发布故事而不登录facebook.i已经经历了很多教程但我找不到任何解决方案。你能帮我解决这个问题。这是我正在使用的代码。

$appid = 'xxxxx'; //Application ID
$appsec = 'xxxxx'; // Application secret
$redirectUrl = 'http://test.com/admin/'; //Facebook redirects back to this page
$permissions = 'publish_stream'; // Permissions we will need



 if(isset($_POST['FacebookPageID']) && strlen($_POST['FacebookPageID'])>10)
 {

 $_SESSION['FacebookPageID']=$_POST['FacebookPageID'];
 $_SESSION['FacebookMessage']=$_POST['FacebookMessage'];
 $_SESSION['FacebookTitle']=$_POST['FacebookTitle'];

 }

 if(!is_numeric($_SESSION['FacebookPageID']) || strlen($_SESSION['FacebookPageID'])<5)
 {

  die("<meta http-equiv=\"refresh\" content=\"2;URL=".$redirectUrl."\" />");
  }
 else
 {

 if(!isset($_GET["code"]))
 {


  $_SESSION['state'] = md5(uniqid(rand(), TRUE));

  $RedirectToFacebook = "https://www.facebook.com/dialog/oauth?client_id=".$appid;
  $RedirectToFacebook .="&redirect_uri=".urlencode($redirectUrl.'test.php');

     die("<script type=\"text/javascript\">top.location.href='" . $RedirectToFacebook .                 </script>
  <noscript><a href='".$RedirectToFacebook."'>Needs Permissioins</a></noscript>");
}
else
{

############## Facebook Page ID ############
$facebookPageID = $_SESSION['FacebookPageID']; 


############## Wall Message ############
$facebookMessage = (empty($_SESSION['FacebookMessage']) ||    s      trlen($_SESSION['FacebookMessage'])<5)?"Nice Facebook Wall Posting     Script!":$_SESSION['FacebookMessage'];

 $facebookTitle = (empty($_SESSION['FacebookTitle']) ||     strlen($_SESSION['FacebookTitle'])<5)?"Nice Facebook Wall Posting  Script!":$_SESSION['FacebookTitle'];


 if($_SESSION['state'] !="")
{
echo $_SESSION['state'];

  // $siteurl=$_GET['realurl'];
  $AccessTokenUrl = "https://graph.facebook.com/oauth/access_token?client_id=".$appid;
  $AccessTokenUrl .="&redirect_uri=".urlencode($redirectUrl.'test.php');
 $AccessTokenUrl .="&client_secret=".$appsec;
 $AccessTokenUrl .="&code=".$_GET["code"];
 $ReturnedString = file_get_contents($AccessTokenUrl);
 $params=null;
 parse_str($ReturnedString, $params);
 $OurAccessToken = $params['access_token']; //access token

 //---------------
 require_once('src/facebook.php' ); //Include our facebook Php Sdk

$post_url = '/'.$facebookPageID.'/feed';
$facebook = new Facebook(array(
'appId' => $appid,
'secret' => $appsec,
 ));

 //the Posting Parameters
$PostData = array(
'message' =>$facebookMessage,
'name' => $_SESSION['title'],
'caption' => "testcom",
'link' => 'from tranz',
'description' => $facebookMessage,
' picture' => "http://test/uploads/".$_SESSION['imageshare']."",
'access_token' =>$OurAccessToken,
'actions' => array(
 array(
'name' => 'Saaraan',
'link' => 'http://www.saaraan.com'
 )
 )
 );

 //print_r($PostData); exit;

 try {
$result = $facebook->api($post_url, 'post', $PostData);
 //$result = $facebook->api('me/feed','post', $PostData);
 if($result)
 {
// session_destroy();
 echo 'Done..';
 die("<meta http-equiv=\"refresh\" content=\"2;URL=".$redirectUrl."?  success=1&fbp=".$facebookPageID."\" />");
  }
 }
 catch (Exception $e)
 {
echo 'Facebook could be experiencing some problem! Try again later <br />Facebook   Says: '. $e->getMessage();
 }
 //--------------
 }
}

1 个答案:

答案 0 :(得分:0)

Facebook要求用户通过身份验证才能发布内容。不,如果认证是“登录”的意思,则无法在没有身份验证的情况下发布。