2012-05-18 62 views
0

我使用这个端点,以获得长期令牌:如何在PHP中保存访问令牌?

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 

但我想知道我可以用PHP得到它。

我需要使用卷曲库吗? 还是有最简单的解决方案?

+0

你应该看看到SDK https://developers.facebook.com/docs/reference/php/ – baloo

+0

没有关于如何检索长寿命令牌。 –

回答

5

这是一个简单的curl函数,用于facebook sdk。不要忘记将路径更改为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; 

} 

这里是方法调用,以获得长寿命令牌:

§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()); 
+0

好,所以我必须使用卷曲,谢谢:D –

+2

很高兴我能帮助你。支持stackoverflow的思想,并回馈给社区,你可以接受一些答案给你的问题。 –