如何在php中保存访问令牌?

Ant*_*tos 0 facebook facebook-graph-api facebook-access-token

我正在使用此端点来获取长期存在的令牌:

https://graph.facebook.com/oauth/access_token?             
    client_id=APP_ID&
    client_secret=APP_SECRET&
    grant_type=fb_exchange_token&
    fb_exchange_token=EXISTING_ACCESS_TOKEN 
Run Code Online (Sandbox Code Playgroud)

但我想知道如何用PHP获取它.

我需要使用curl库吗?或者有最简单的解决方案吗?

Sas*_*ley 5

这是一个与facebook sdk一起使用的简单curl函数.不要忘记改变路径fb_ca_chain_bundle.crt.

function curl($url, $certificate = false) {

    $c = curl_init($url);

    curl_setopt($c, CURLOPT_HTTPGET, true);
    curl_setopt($c, CURLOPT_FRESH_CONNECT, true);
    curl_setopt($c, CURLOPT_RETURNTRANSFER, true);

    curl_setopt ($c, CURLOPT_SSL_VERIFYPEER, TRUE); 
    curl_setopt ($c, CURLOPT_CAINFO, dirname(__FILE__) . '/sdk/fb_ca_chain_bundle.crt');

    $output = curl_exec($c);

    if ($output === false) {
        curl_close($c);
        return false;
    }

    curl_close($c);
    return $output;

}
Run Code Online (Sandbox Code Playgroud)

以下是获取长期令牌的方法调用:

§token = curl('https://graph.facebook.com/oauth/access_token?client_id='.
               $app_id.'&client_secret='.
               $app_secret.'&grant_type=fb_exchange_token&fb_exchange_token='.
               $api->getAccessToken());
Run Code Online (Sandbox Code Playgroud)

  • 很高兴我能帮助你.为了支持stackoverflow的思想并回馈社区,你可以接受一些问题的答案. (2认同)