2012-05-18 110 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的思想,並回饋給社區,你可以接受一些答案給你的問題。 –