// Teknoloji

Sending Automatic Posts to a Facebook Page with PHP Coding

Hello,

In this post, I’ll try to explain how to automatically send posts to our existing Facebook page using PHP coding. For this, I assume that you first have a site where the PHP code will run and a page that already exists on Facebook. Before starting the detailed explanation, if we roughly summarize all the steps,

first we need to do the Facebook operations. First, we create a Page on Facebook, then we create an App and make the necessary permission settings, and finally we complete the Access Token retrieval steps. After we finish our work on the Facebook side, we do our PHP coding.

  • Since creating a Facebook Page is very easy and is shown in many sources, I’m skipping this step.
  • HERE By clicking the Green Button (“Download the SDK for PHP v5.0”), we need to download the Facebook files and put them in the directory where we will run PHP. Among these files, we will use “autoload.php”.
  • You can review our article on Creating a Facebook App HERE .
  • You can review our article on Getting a Long-Lived Access Token HERE .

After completing the steps above, we paste the following function into our PHP page.

function FacePaylas($face_url,$face_adi,$face_aciklama,$face_picture,$face_caption,$face_message) {

  require_once("../facebook-sdk/src/Facebook/autoload.php");
  $fb = new FacebookFacebook([
   'app_id' => 'FACEBOOK_UYGULAMA_ID',
   'app_secret' => 'FACEBOOK_UYGULAMA_GIZLI_KODU',
   'default_graph_version' => 'v2.8',
  ]);

  $linkData = [
   'link' => $face_url,
   'picture' => $face_picture,
   'caption' => $face_caption,
   'description' => $face_aciklama,
   'name' => $face_adi,
   'message' => $face_message
  ];

  $pageAccessToken='LONG_LIVED_ACCESS_TOKEN';
  try {
   $response = $fb->post('/FACEBOOK_SAYFA_ID/feed', $linkData, $pageAccessToken);
  } catch(FacebookExceptionsFacebookResponseException $e) {
   echo 'Graph returned an error: '.$e->getMessage();
   exit;
  } catch(FacebookExceptionsFacebookSDKException $e) {
   echo 'Facebook SDK returned an error: '.$e->getMessage();
   exit;
  }
  $graphNode = $response->getGraphNode();

echo "<div>Face OK</div>";

} //func

 

With the following code, we can call our function.

FacePaylas('http://www.xxx.com/deneme.html','Ekleyen','Açıklama','Foto_URL','Başlık','Mesaj');

 

Let’s recall again where to get the key values.

FACEBOOK_UYGULAMA_GIZLI_KODU -> The key under the “App Secret” heading that appears when we create a Facebook app.

FACEBOOK_UYGULAMA_ID -> The key under the “App ID” heading that appears when we create a Facebook app.

LONG_LIVED_ACCESS_TOKEN -> I explained it in my article on getting a Long-Lived Access Token from Facebook. Here

FACEBOOK_SAYFA_ID -> The numbers written in the address bar when we create a Facebook page, or the value of the “Profile ID” field that appears in the last step of getting a Long-Lived Access Token from Facebook.

If you have applied all the steps completely, you can make automatic posts on your Facebook page.

Please share with attribution.

// FEEDBACK

Was this useful?

Your rating shows how helpful this note is to other readers.

Was this useful?

Click the stars to rate.

Average rating 0 / 5. Total votes: 0

No one has rated this post yet. Be the first!

Sorry this post was not helpful.

Your feedback is valuable to us.

How can we make this post more helpful?

// JOIN THE CONVERSATION

Share your experience

Got a question, a different solution or a note to add?

// KEEP READING

One more note.

All posts ↗︎