2012-09-17 43 views
0

這裏是我用來從我的網站發佈到facebook頁面牆的故事的代碼。我已經安裝了facebook sdk,如facebook.php和base_facebook.php等files.My的要求是發佈的故事沒有登錄到facebook.i已經通過許多教程,但我找不到任何解決方案。cancan請你幫我在這。這裏是我正在使用的代碼。是否可以從我的網站向Facebook頁面牆張貼故事而無需登錄Facebook?

$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(); 
} 
//-------------- 
} 
} 
+2

我真的,真的不明白什麼,太亂了,格式化你的代碼。 –

回答

0

Facebook要求用戶進行身份驗證才能發佈內容。不,如果身份驗證是您「登錄」的意思,則無法進行身份驗證。

+0

如果應用程序有權在某人的牆上寫信,則可以發佈。 –

+0

應用程序必須通過API對其自身進行身份驗證。 – rationalboss

+0

是的,但這不是「登錄」,而只是傳遞應用訪問令牌。 – CBroe

相關問題