Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2><strong>Update:</strong> Offline Access in <a href="https://developers.facebook.com/roadmap/offline-access-removal/" rel="nofollow">now deprecated</a>. Use something else instead.</h2> <hr> <p>If you want to use the access token of the user even after he has logged out of your app, you have to ask for the <code>offline_access</code> permission.</p> <p>With the Facebook PHP SDK v3 (<a href="https://github.com/facebook/php-sdk" rel="nofollow">see on github</a>), it is pretty simple. To log someone with the <code>offline_access</code> permission, you ask it when your generate the login URL. Here is how you do that.</p> <h1>Get the offline access token</h1> <p>First you check if the user is logged in or not :</p> <pre><code>require "facebook.php"; $facebook = new Facebook(array( 'appId' =&gt; YOUR_APP_ID, 'secret' =&gt; YOUR_APP_SECRET, )); $user = $facebook-&gt;getUser(); if ($user) { try { $user_profile = $facebook-&gt;api('/me'); } catch (FacebookApiException $e) { $user = null; } } </code></pre> <p>If he is not, you generate the "Login with Facebook" URL asking for the <code>offline_access</code> permission :</p> <pre><code>if (!$user) { $args['scope'] = 'offline_access'; $loginUrl = $facebook-&gt;getLoginUrl($args); } </code></pre> <p>And then display the link in your template :</p> <pre><code>&lt;?php if (!$user): ?&gt; &lt;a href="&lt;?php echo $loginUrl ?&gt;"&gt;Login with Facebook&lt;/a&gt; &lt;?php endif ?&gt; </code></pre> <p>Then you can retrieve the offline access token and store it. To get it, call :</p> <pre><code>if ($user) { $token = $facebook-&gt;getAccessToken(); // store token } </code></pre> <h1>Use the offline access token</h1> <p>To use the offline access token when the user is not logged in :</p> <pre><code>require "facebook.php"; $facebook = new Facebook(array( 'appId' =&gt; YOUR_APP_ID, 'secret' =&gt; YOUR_APP_SECRET, )); $facebook-&gt;setAccessToken("..."); </code></pre> <p>And now you can make API calls for this user :</p> <pre><code>$user_profile = $facebook-&gt;api('/me'); </code></pre> <p>Hope that helps !</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload