在页面应用程序中向用户发送带有FB和PHP SDK signedRequest的电子邮件

时间:2013-02-25 19:21:47

标签: facebook email facebook-php-sdk

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

我有几个问题。

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

  2. 即使我要求提供电子邮件权限,但signed_request也没有提供此类信息。

  3. 请记住,我是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
    )
    

1 个答案:

答案 0 :(得分:1)

YES!终于做到了!!现在只有电子邮件部分。代码有点凌乱,需要清理,但它的工作原理:)答案被发现here

//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 . "!");
}