保存Facebook的signed_request?

时间:2011-02-21 13:38:55

标签: php facebook

情况
我在页面上玩新的Facebook iFrame标签。我正在使用signed_request获取有关应用程序,页面和用户的一些事实。

挑战
Facebook仅在第一次调用iframe时通过$ _REQUEST提供signed_request参数。问题是第二次访问parms,例如对于ajax请求。

我就这样做了
我像这样扩展了PHP SDK,将signed_request存储在cookie中。

/**
 * The name of the Cookie that contains the session.
 * @return String the cookie name
 */
protected function getSessionSignedRequestCookieName() {
    return 'fbs_sr_' . $this->getAppId();
}

/**
 * Saves the signed_request as a browser coookie
 * @return void;
 */
protected function saveSignedRequestAsCookie($signedRequest) {
    $cookieName = $this->getSessionSignedRequestCookieName();

    setcookie($cookieName, serialize($signedRequest));
}

/**
 * Returns the SigndRequest from $_COOKIE
 * @return array
 */
public function getSignedRequestFromCookie() {
    $cookieName = $this->getSessionSignedRequestCookieName();

    if(isset($_COOKIE[$cookieName])) {
        $this->signedRequest = $_COOKIE[$cookieName];
        return unserialize($this->signedRequest);
    } else {
        return NULL;
    }
}

/**
 * Returns the data from a signed_request token provided by $_REQUEST or $_SESSION
 * @return array The decoede signed Request
 */
public function getSignedRequest() {
    $signedRequest = parent::getSignedRequest();

    if($signedRequest != NULL) {
        $this->saveSignedRequestAsCookie($signedRequest);
        return $signedRequest;
    } else {
        return $this->getSignedRequestFromCookie();
    }
}

您的想法 * 我是一个好主意还是一个糟糕的解决方法?有什么建议/改进吗?

1 个答案:

答案 0 :(得分:1)

建议的解决方案对我有一个小缺陷:unserialize返回一个空对象,所以我保存了签名请求的base64编码json格式(Facebook发送到画布应用程序)