title: PHP Facebook SDK 4.0 post as a page server to server
body: <p>I'm trying to post as a page on a Facebook page since 6 days without success, which is really frustrating using new facebook SDK 4.0.4.</p>
  
  <p>I'm under PHP 5.4 (as requested by Facebook 4.0 SDK version).
  I already read some times all the facebook documentation (which isn't so clear) <a href="https://developers.facebook.com/docs/reference/php/4.0.0" rel="nofollow">https://developers.facebook.com/docs/reference/php/4.0.0</a>.</p>
  
  <p>For logging-in:</p>
  
  <pre><code>&lt;?php
  
  require_once( BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookSession.php' );
  require_once( BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookRedirectLoginHelper.php' );
  require_once( BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookRequest.php' );
  require_once( BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookResponse.php' );
  require_once( BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookSDKException.php' );
  require_once( BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookRequestException.php' );
  require_once( BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookOtherException.php' );
  require_once( BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookAuthorizationException.php' );
  require_once( BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/GraphObject.php' );
  require_once( BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/GraphSessionInfo.php' );
  require_once( BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookServerException.php' );
  
  use Facebook\FacebookSession;
  use Facebook\FacebookRedirectLoginHelper;
  use Facebook\FacebookRequest;
  use Facebook\FacebookResponse;
  use Facebook\FacebookSDKException;
  use Facebook\FacebookRequestException;
  use Facebook\FacebookOtherException;
  use Facebook\FacebookAuthorizationException;
  use Facebook\GraphObject;
  use Facebook\GraphSessionInfo;
  use Facebook\FacebookServerException;
  
  
  
          session_start();
  
  
          // init app with app id and secret
          FacebookSession::setDefaultApplication($site["facebook_appid"],$site["facebook_appsecret"] );
  
          // login helper with redirect_uri
          $helper = new FacebookRedirectLoginHelper($site["url"].$_SERVER['REQUEST_URI']);
  
          // see if a existing session exists
          if ( isset( $_SESSION ) &amp;&amp; isset( $_SESSION['fb_token'] ) ) {
            // create new session from saved access_token
            $session = new FacebookSession( $_SESSION['fb_token'] );
  
            // validate the access_token to make sure it's still valid
            try {
              if ( !$session-&gt;validate() ) {
                $session = null;
              }
            } catch ( Exception $e ) {
              // catch any exceptions
              $session = null;
            }
             echo '&lt;a href="' . $helper-&gt;getLogoutUrl( $session, 'http://yourwebsite.com/app/logout.php' ) . '"&gt;Logout&lt;/a&gt;';
          } else {
            // no session exists
  
            try {
              $session = $helper-&gt;getSessionFromRedirect();
            } catch( FacebookRequestException $ex ) {
              // When Facebook returns an error
            } catch( Exception $ex ) {
              // When validation fails or other local issues
              echo $ex-&gt;message;
            }
  
            echo '&lt;a href="' . $helper-&gt;getLoginUrl( array( 'public_profile', 'email', 'user_friends' , 'publish_stream', 'manage_pages', 'publish_actions', 'status_update', 'user_likes ',  'photo_upload') ) . '"&gt;Login&lt;/a&gt;';
  
  
          }
  
  
  
  ///////////////////////////
  // SHORT LIVED TOKEN
    $request = new FacebookRequest( $session, "GET", "/".$site["facebook_appid"]."/accounts?access_token=".$site["facebook_appid"]."|".$site["facebook_appsecret"]."&amp;grant_type=client_credentials" );
    $response = $request-&gt;execute();
    // get response
    $graphObject = $response-&gt;getGraphObject()-&gt;asArray();
  
  
    if (DebugMode) e($graphObject);
  
      $site['facebook_short-lived_access_token'] =  $graphObject['data'][0]-&gt;access_token;
  
  ////////////////////////
  
  ///////////////////////////
  // LONG LIVED TOKEN
    $request = new FacebookRequest( $session, "GET", "/oauth/access_token?grant_type=fb_exchange_token&amp;client_id=".$site["facebook_appid"]."&amp;client_secret=".$site["facebook_appsecret"]."&amp;fb_exchange_token=" .$site['facebook_short-lived_access_token']);
    $response = $request-&gt;execute();
    // get response
    $graphObject = $response-&gt;getGraphObject()-&gt;asArray();
  
  
    if (DebugMode) e($graphObject);
  
      $site['facebook_long-lived_access_token'] =  $graphObject['access_token'];
      $_SESSION['fb_token'] = $site['facebook_long-lived_access_token'];
  
      if (DebugMode) e($site['facebook_long-lived_access_token']);
  
  ?&gt;
  </code></pre>
  
  <p>And for publishing:</p>
  
  <pre><code>&lt;?php
  
  require_once( BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookRequest.php' );
  require_once( BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/GraphObject.php' );
  require_once( BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookSDKException.php' );
  require_once( BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookRequestException.php' );
  require_once( BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookPermissionException.php' );
  
  
  use Facebook\FacebookRequest;
  use Facebook\GraphObject;
  use Facebook\FacebookRequestException;
  use Facebook\FacebookSDKException;
  use Facebook\FacebookPermissionException;
  
  
  if($session) {
  
  
    try {
      $response = (new FacebookRequest(
        $session, 'POST', "/".$site["facebook_page_id"]."/feed",
        array(
          'access_token' =&gt; $_SESSION['fb_token'],
          'link' =&gt; $site["url"].'/url',
          'name' =&gt; 'link name',
          'caption' =&gt; 'link caption',
          'description' =&gt; 'link description',
          'picture' =&gt; $site["url"].'/alpha.png',
          'message' =&gt; 'User provided message'
        )
      ))-&gt;execute()-&gt;getGraphObject();
  
      echo "Posted with id: " . $response-&gt;getProperty('id');
  
    } catch(FacebookRequestException $e) {
  
      echo "Exception occured, code: " . $e-&gt;getCode();
      echo " with message: " . $e-&gt;getMessage();
  
    }   
  
  }
  
  ?&gt;
  </code></pre>
  
  <p>I first get a session ID, then request and obtain a short-lived token, with which i request and obtain a long-lived token (60 days validity). Then I continue with the publish script using the last obtained token.
  I submit a post and it return back a post-id like it works, but nothing on the page, nothing in my personal Facebook history. </p>
  
  <p>My application is in development mode and was not reviewed by Facebook, but it seems to not be necessary anymore: <a href="http://www.nextscripts.com/known-issues/facebook-issues/" rel="nofollow">http://www.nextscripts.com/known-issues/facebook-issues/</a></p>
  
  <p>Any idea where I'm wrong?</p>