title: Post on Facebook profile/page timeline using Facebook Graph API v3.1 PHP SDK
body: <p>As you know that Facebook introduced new Graph API v3.1 on July 26, 2018 that changed many things for developers. One of the questions I have now is how to share/post on Facebook profile/page timeline using Facebook Graph API v3.1 PHP SDK?</p>
  
  <blockquote>
    <p>Reminder: Graph API v2.7 will be deprecated on Oct 05, 2018. Please use the <a href="https://developers.facebook.com/tools/api_versioning/" rel="nofollow noreferrer">API Upgrade Tool</a> to understand how this might impact your app. For more details see the <a href="https://developers.facebook.com/docs/graph-api/changelog" rel="nofollow noreferrer">changelog</a></p>
  </blockquote>
  
  <p>For this I created a new apps with some settings as shown in the below screenshots.</p>
  
  <p><a href="https://i.stack.imgur.com/fH1b4.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fH1b4.jpg" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/veiu7.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/veiu7.jpg" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/QocoR.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QocoR.jpg" alt="enter image description here"></a></p>
  
  <p>For this purpose, I used the below mentioned code along with <a href="https://github.com/facebook/php-graph-sdk" rel="nofollow noreferrer">facebook-php-graph-sdk-5.x</a>.</p>
  
  <h2>fbConfig.php</h2>
  
  <pre><code>&lt;?php
  if(!session_id()){
      session_start();
  }
  
  // Include the autoloader provided in the SDK
  require_once __DIR__ . '/facebook-php-graph-sdk-5.x/autoload.php';
  
  // Include required libraries
  use Facebook\Facebook;
  use Facebook\Exceptions\FacebookResponseException;
  use Facebook\Exceptions\FacebookSDKException;
  
  /*
   * Configuration and setup Facebook SDK
   */
  $appId         = 'APP_ID'; //Facebook App ID
  $appSecret     = 'APP_SECRET'; //Facebook App Secret
  $redirectURL   = 'MAIN_PAGE_URL_SAME_AS_IN_APPS_SETTING'; //Callback URL
  $fbPermissions = array('publish_actions'); //Facebook permission
  
  $fb = new Facebook(array(
      'app_id' =&gt; $appId,
      'app_secret' =&gt; $appSecret,
      'default_graph_version' =&gt; 'v2.6',
  ));
  
  // Get redirect login helper
  $helper = $fb-&gt;getRedirectLoginHelper();
  
  // Try to get access token
  try {
      if(isset($_SESSION['facebook_access_token'])){
          $accessToken = $_SESSION['facebook_access_token'];
      }else{
          $accessToken = $helper-&gt;getAccessToken();
      }
  } catch(FacebookResponseException $e) {
       echo 'Graph returned an error: ' . $e-&gt;getMessage();
        exit;
  } catch(FacebookSDKException $e) {
      echo 'Facebook SDK returned an error: ' . $e-&gt;getMessage();
        exit;
  }
  ?&gt;
  </code></pre>
  
  <h2>index.php</h2>
  
  <pre><code>&lt;?php
  // Include FB configuration file
  require_once 'fbConfig.php';
  
  if(isset($accessToken)){
      if(isset($_SESSION['facebook_access_token'])){
          $fb-&gt;setDefaultAccessToken($_SESSION['facebook_access_token']);
      }else{
          // Put short-lived access token in session
          $_SESSION['facebook_access_token'] = (string) $accessToken;
  
          // OAuth 2.0 client handler helps to manage access tokens
          $oAuth2Client = $fb-&gt;getOAuth2Client();
  
          // Exchanges a short-lived access token for a long-lived one
          $longLivedAccessToken = $oAuth2Client-&gt;getLongLivedAccessToken($_SESSION['facebook_access_token']);
          $_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;
  
          // Set default access token to be used in script
          $fb-&gt;setDefaultAccessToken($_SESSION['facebook_access_token']);
      }
  
      //FB post content
      $message = 'Test message from stackoverflow.com website';
      $title = 'Post From Website';
      $link = 'http://www.stackoverflow.com/';
      $description = 'stackoverflow is simply awesome.';
      $picture = 'https://i.stack.imgur.com/MybMA.png';
  
      $attachment = array(
          'message' =&gt; $message,
          'name' =&gt; $title,
          'link' =&gt; $link,
          'description' =&gt; $description,
          'picture'=&gt;$picture,
      );
  
      try{
          // Post to Facebook
          $fb-&gt;post('/me/feed', $attachment, $accessToken);
  
          // Display post submission status
          echo 'The post was published successfully to the Facebook timeline.';
      }catch(FacebookResponseException $e){
          echo 'Graph returned an error: ' . $e-&gt;getMessage();
          exit;
      }catch(FacebookSDKException $e){
          echo 'Facebook SDK returned an error: ' . $e-&gt;getMessage();
          exit;
      }
  }else{
      // Get Facebook login URL
      $fbLoginURL = $helper-&gt;getLoginUrl($redirectURL, $fbPermissions);
  
      // Redirect to Facebook login page
      echo '&lt;a href="'.$fbLoginURL.'"&gt;&lt;img src="https://www.freeiconspng.com/uploads/facebook-login-button-png-11.png" /&gt;&lt;/a&gt;';
  }
  
  ?&gt;
  </code></pre>
  
  <p>Where as my files level are as shown in the below screenshot.</p>
  
  <p><a href="https://i.stack.imgur.com/nNsrD.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nNsrD.jpg" alt="enter image description here"></a></p>
  
  <p>Finally after doing all of the above setting and codes, when I try to run my page then I got LOGIN FACEBOOK BUTTON and after clicking there, I got the below screenshot error.</p>
  
  <p><a href="https://i.stack.imgur.com/Lk34D.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Lk34D.png" alt="enter image description here"></a></p>
  
  <p>What I want is simple post my desired  content without showing any POPUP or dialog so that I can easily use it via my PHP Codes.</p>
  
  <p>I tried to find any working solution over internet but all are now old, nothing is now working with new Facebook Graph API v3.1.</p>