How to create a Facebook Group Posting in Curl PHP

Here’s a PHP cURL example to post to a Facebook group using the Facebook Graph API:

$groupId = 'YOUR_GROUP_ID';
$accessToken = 'YOUR_ACCESS_TOKEN';
$message = 'Your message to post';
$url = "https://graph.facebook.com/{$groupId}/feed";

$fields = [
  'message' => $message,
  'access_token' => $accessToken
];

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);

if ($response) {
  $result = json_decode($response, true);
  echo 'Post successful:', print_r($result, true);
} else {
  echo 'Error posting:', curl_error($ch);
}

Make sure to replace `’YOUR_GROUP_ID’` with the actual group ID and `’YOUR_ACCESS_TOKEN‘` with your obtained access token.

This code uses the PHP cURL library to make a POST request to the Facebook Graph API endpoint for group posts. The message content is included as a parameter in the request, along with the access token.

After executing the cURL request, it checks the response and if successful, it outputs the returned data, which would include the post information. Otherwise, if an error occurs, it displays the corresponding error message.

Remember to refer to the Facebook Graph API documentation for more detailed information on available parameters and options, and to ensure your app has the necessary permissions to post to the group.

Leave a Reply

Your email address will not be published. Required fields are marked *