Facebook签署请求获取UID PHP

时间:2012-06-05 07:52:49

标签: php facebook-graph-api

require_once 'library/facebook/facebook.php';

$facebook = new Facebook(array(
'appId'  => 'appid',
'secret' => 'secret',
));

$signed_request = $_REQUEST["signed_request"];
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);

如何在上述代码之后获取UID。

2 个答案:

答案 0 :(得分:1)

你不需要为此包含facebook api。就此而言,

$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
echo $data["user_id"];

这是$data数组中其他信息的完整列表。

{
  "algorithm": "HMAC-SHA256",
  "expires": 1317243600,
  "issued_at": 1317239909,
  "oauth_token": "<token>",
  "page": {
    "id": "<id>",
    "liked": false,
    "admin": true
  },
  "user": {
    "country": "in",
    "locale": "en_GB",
    "age": {
      "min": 21
    }
  },
  "user_id": "<user-id>"
}

答案 1 :(得分:0)

因为您使用的是php sdk,

你可以简单地说

$user = $facebook->getUser();

PHP sdk将从signed_request中读取uid。

您可以直接从此签名的请求中获取

$signed_request = $facebook->getSignedRequest();
$user = isset($signed_request['user_id']) ? $signed_request['user_id'] : '';