title: PHP Facebook get Fan Page token
body: <p>I constructed the following PHP code below after following some Q&amp;As here. The code below generates an indefinate (two months) Access token for my Facebook App.</p>
  
  <p>Basically by Facebook App lets users upload multiple pictures to their specific album on my facebook page. For this app to work it needs two access tokens. The App access token (the code below) and the Page Access token.</p>
  
  <p>I use a cron job to generate the first access token to a text file (the code below) and it works fine. Now I'd like to generate the page access token using the similar code below to another text file but I haven't been sucessful.</p>
  
  <pre><code>&lt;?php
  
  require_once 'library/facebook.php';
  include '../constants.php';
  
  define('TOKEN_FILE', 'fb_app_token.txt'); // dont need to change this
  
  
  $fb = new Facebook(array(
      'appId' =&gt; FB_APP_ID,
      'secret' =&gt; FB_APP_SECRET,
      'cookie' =&gt; true,
  ));
  
  
  function ensure_login($fb) {
      if (!$fb-&gt;getUser()) {
          header('Location: '.$fb-&gt;getLoginUrl(array(
              'scope' =&gt; 'manage_pages,publish_stream',
          )));
          die();
      }
  }
  
  function fetch_long_living_access_token($fb) {
      $qs = http_build_query(array(
          'client_id'         =&gt; $fb-&gt;getAppId(),
          'client_secret'     =&gt; $fb-&gt;getApiSecret(),
          'grant_type'        =&gt; 'fb_exchange_token',
          'fb_exchange_token' =&gt; $fb-&gt;getAccessToken(),
      ));
  
      $fb_resp = file_get_contents('https://graph.facebook.com/oauth/access_token?'.$qs);
      if (!$fb_resp) {
          ensure_login($fb);
      }
      parse_str($fb_resp, $fb_resp);
      if (!isset($fb_resp['access_token'])) {
          ensure_login($fb);
      }
      $fb-&gt;setAccessToken($fb_resp['access_token']);
      return $fb_resp['access_token'];
  
  }
  
  function save_page_access_token($token) {
      file_put_contents(TOKEN_FILE, $token);
  }
  
  function get_random_message($source) {
      $lines = preg_split('/[\n\r]+/', trim(file_get_contents($source)));
      return $lines[array_rand($lines)];
  }
  
  
  ensure_login($fb);
  $page_access_token = fetch_long_living_access_token($fb);
  save_page_access_token($page_access_token);
  print 'saved access token: '.$page_access_token;
  
  
  ?&gt;
  </code></pre>
  
  <p>What I tried till now:</p>
  
  <pre><code>&lt;?php
  require_once 'library/facebook.php';
  include '../constants.php';
  define('TOKEN_FILE', 'fb_page_accestoken.txt'); // dont need to change this
  $fb = new Facebook(array(
      'appId' =&gt; FB_APP_ID,
      'secret' =&gt; FB_APP_SECRET,
      'cookie' =&gt; true,
  ));
  $access_token = file_get_contents('fb_app_token.txt');
  $response  = "https://graph.facebook.com/me/accounts?access_token=".$access_token;
  echo $response;
  ?&gt;
  </code></pre>
  
  <p>This code below. Basically it gets the above user token and it gives me a URL to get the indefinate (2 months) Facebook Page access code.</p>
  
  <p>I enter this URL in the brower and it gives me arrays of all the pages I have. In this case I select the access token of my corresponding page.</p>
  
  <p>I would like to get the access token of the page similar to the first code - rather than using my second code. Or else, get the page access code from the arrays of the second code.</p>
  
  <p>I was unable to find how to get specific data from the array given by facebook api such as the page access code ONLY.</p>