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.

Share your experience
Got a question, a different solution or a note to add?
Log in or join us to write a comment.
Log in ↗︎Sign up ↗︎