2011-08-19 118 views
2

我正在嘗試爲Facebook準備應用程序。應用軟件被創建。 「localhost」測試沒有任何問題,但Facebook應用程序中的鏈接無法正常工作... page.php?page = test ...應用程序,使用粉絲頁面。第一頁打開,用戶IDPageID數據是無縫的,但我點擊鏈接到示例,數據是空白的。我試圖目標=「_頂」但是不行......facebook應用程序鏈接到問題

require 'facebook.php'; 
$facebook = new Facebook(array(
    'appId' => 'xxx', 
    'secret' => 'xxx', 
    'cookie' => false, 
)); 
$signed_request = $_REQUEST["signed_request"]; 
list($encoded_sig, $payload) = explode('.', $signed_request, 2); 
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true); 
$m2_facebook_kullanici_id = $data['user_id']; 
$m2_facebook_sayfa_id = $data['page']['id']; 
if(empty($_GET['sayfa'])){ 
    ...working here USERID and PAGEID... 
} 
if($_GET['sayfa'] == 'guncelle'){ 
    ...does not work here USERID and PAGEID... 
} 

我不知道什麼是錯的? 謝謝...


基本解決:

<?php 
require 'facebook.php'; 
$signed_request = $_REQUEST["signed_request"]; 
$facebook = new Facebook(array(
    'appId' => 'xxx', 
    'secret' => 'xxx', 
    'cookie' => false, 
)); 
list($encoded_sig, $payload) = explode('.', $signed_request, 2); 
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true); 

$m2_facebook_kullanici_id = $data['user_id']; 
$m2_facebook_sayfa_id = $data['page']['id']; 

setcookie("facebook_user_id", $m2_facebook_kullanici_id); 
setcookie("facebook_page_id", $m2_facebook_sayfa_id); 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Title</title> 
</head> 
<body> 
<?php 
if(empty($_GET['sayfa'])){ 
    echo '<a href="index.php?sayfa=guncelle">Güncelle</a><br />'; 
    echo '<h3>Home Page</h3>'; 
    echo '<strong>User ID:</strong> ' . $m2_facebook_kullanici_id . '<br />'; 
    echo '<strong>Page ID:</strong> ' . $m2_facebook_sayfa_id; 
} 
if($_GET['sayfa'] == 'guncelle'){ 
    $m2_facebook_kullanici_id = $_COOKIE["facebook_user_id"]; 
    $m2_facebook_sayfa_id = $_COOKIE["facebook_page_id"]; 
    echo '<a href="index.php">Back</a><br />'; 
    echo '<h3>Settings Page</h3>'; 
    echo '<strong>User ID:</strong> ' . $m2_facebook_kullanici_id . '<br />'; 
    echo '<strong>Page ID:</strong> ' . $m2_facebook_sayfa_id; 
} 
?> 
</body> 
</html> 

回答

2

SOLUTION:

只是將下面的代碼添加到鏈接的末尾

?signed_request=' . $_REQUEST['signed_request'] . ' 

&amp;signed_request=' . $_REQUEST['signed_request'] . ' 

的應用程序的一個簡單的例子

<?php 
require 'facebook.php'; 
function parse_signed_request($signed_request, $secret) { 
    list($encoded_sig, $payload) = explode('.', $signed_request, 2); 
    $sig = base64_url_decode($encoded_sig); 
    $data = json_decode(base64_url_decode($payload), true); 
    if (strtoupper($data['algorithm']) !== 'HMAC-SHA256') { 
    error_log('Unknown algorithm. Expected HMAC-SHA256'); 
    return null; 
    } 
    $expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true); 
    if ($sig !== $expected_sig) { 
    error_log('Bad Signed JSON signature!'); 
    return null; 
    } 
    return $data; 
} 
function base64_url_decode($input) { 
    return base64_decode(strtr($input, '-_', '+/')); 
} 
$data = parse_signed_request($_REQUEST['signed_request'], 'a5aaf146542d50c11f55e001c0fb8f31'); 

$m2_facebook_kullanici_id = $data['user_id']; 
$m2_facebook_sayfa_id = $data['page']['id']; 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Title</title> 
</head> 
<body> 
<?php 
if(empty($_GET['sayfa'])){ 
    echo '<a href="app_fan_page.php?sayfa=guncelle&amp;signed_request='.$_REQUEST['signed_request'].'">Settings</a><br />'; 
    echo '<h3>Home</h3>'; 
    echo '<strong>User ID:</strong> ' . $m2_facebook_kullanici_id . '<br />'; 
    echo '<strong>Page ID:</strong> ' . $m2_facebook_sayfa_id; 
} 
if($_GET['sayfa'] == 'guncelle'){ 
    echo '<a href="app_fan_page.php?signed_request=' . $_REQUEST['signed_request'] . '">Home</a><br />'; 
    echo '<h3>Settings</h3>'; 
    echo '<strong>User ID:</strong> ' . $m2_facebook_kullanici_id . '<br />'; 
    echo '<strong>Page ID:</strong> ' . $m2_facebook_sayfa_id; 
} 
?> 
</body> 
</html> 

特別感謝:moguzalp和梅蘇特Eyrice