0

最近我开始学习 PHP,因为我想做一个简单的应用程序并学习新的东西。但现在我正在努力与 Facebook 集成。

我有几个问题。

  1. 当我单击 href 以获取 perms 时 - 没有任何反应 - 即使设置了显示弹出参数,也必须在新选项卡中打开它。(需要使用 iframe 和访问令牌?)

  2. 尽管我要求电子邮件许可,但 signed_request 并未携带此类信息。

请记住,我是 PHP 的新手。谢谢大卫

这是我的代码:

//call facebook extended perms.
$loginUrl = $facebook->getLoginUrl($params);
$params = array(
'client_id' => 'AppID',
'scope' => 'email, publish_stream',
);

$loginUrl = $facebook->getLoginUrl($params);

echo ('a href="'.$loginUrl.'" perms a');

这是signed_request

//Get the signed request
$srq = $_REQUEST['signed_request'];

function parse_signed_request($srq) {
  list($encoded_sig, $payload) = explode('.', $srq, 2); 

  // decode the data
  $sig = base64_url_decode($encoded_sig);
  $data = json_decode(base64_url_decode($payload), true);

  print_r ($data);
}

function base64_url_decode($input) {
  return (base64_decode(strtr($input, '-_', '+/')));
}

//print the signed request for "debug?"
parse_signed_request($srq);`

当我通过烫发时,输出如下。

Array
(
    [algorithm] => HMAC-SHA256
    [expires] => 1361822400
    [issued_at] => 1361817488
    [oauth_token] => loooooong token
    [page] => Array
        (
            [id] => 146945218690274
            [liked] => 1
            [admin] => 1
        )

    [user] => Array
        (
            [country] => cz
            [locale] => cs_CZ
            [age] => Array
                (
                    [min] => 18
                    [max] => 20
                )

        )

    [user_id] => deleted
)
4

1 回答 1

1

是的!终于成功了!!现在只有电子邮件部分。代码有点乱,需要清理,但它可以工作:) 答案是在这里找到的。

//include facebook.php
require_once("./src/facebook.php");

$config = array();
$config[‘appId’] = '123222421192165';
$config[‘secret’] = '346406ef4af8382c71764c4fd270b595';
$config[‘fileUpload’] = false; // optional
$app_FacebookURL = 'https://still-mesa-9778.herokuapp.com';
$facebook = new Facebook($config);
$params = array(
'client_id' => '123222421192165',
'scope' => 'email, publish_stream',
);

$loginUrl = $facebook->getLoginUrl($params);

//test signed postu
$srq = $_REQUEST['signed_request'];

function parse_signed_request($srq) {
list($encoded_sig, $payload) = explode('.', $srq, 2); 

// decode the data
$sig = base64_url_decode($encoded_sig);
$data = json_decode(base64_url_decode($payload), true);
$GLOBALS['sdata'] = $data; //create a superglobal variable to hold the decoded data for later use
print_r ($data);
}

function base64_url_decode($input) {
return (base64_decode(strtr($input, '-_', '+/')));
}

//print the signed post
parse_signed_request($srq);


//Check for oauth_token in signed request.
if (!array_key_exists('oauth_token', $sdata))
{
    //There is no oauth_token in the signed request so forward to authorizing page.
    echo "<script>top.location.href='https://graph.facebook.com/oauth/authorize?client_id=".$params['client_id']."&redirect_uri=". urlencode($app_FacebookURL)."&scope=".$params['scope']."'</script>";
}
else //if (array_key_exists('oauth_token', $signedRequest))
{
    //There is an oauth_token in the signed request.  For now just print it...
    echo("oauth_token is: ");
    echo($sdata['oauth_token']);

    //... and also use it to fetch the user's email then print the user's email so I know it's working.
    $graph_url = "https://graph.facebook.com/me?access_token=" . $sdata['oauth_token'];
    $user = json_decode(file_get_contents($graph_url));
    echo("\nHello " . $user->email . "!");
}
于 2013-02-26T13:26:02.657 回答