I am trying to get multiple images from the user and post it on the Facebook page using Facebook Graph API Javascript SDK. Currently, I am able to post a single image. I searched facebook documentation and got nothing except it may be possible through albums, batch requests, or adding an unpublished post and then publishing it. But I don't know about any of them exactly. Your help will be highly appreciated. Following is the code used to publish a single image:
function postImage()
{
$uploadDirectory = "uploadedFiles/";
$filename = $_FILES["postImage"]["name"];
$extension = pathinfo($filename, PATHINFO_EXTENSION);
$uniqueName = uniqid("UP-IMG-", true) . "." . $extension;
$destinationPath = $uploadDirectory . $uniqueName;
move_uploaded_file($_FILES["postImage"]["tmp_name"], $destinationPath);
$_GET['imageUrl'] = "https://www.salebazarmzd.com/" . $destinationPath;
//creating variables to store data from Super Global Vars
$url = $_GET['imageUrl'];
$message = $_POST['postDescription'];
$link = $_POST['postLink'];
$message = $message . "\n" . $link;
$page_access_token = $_GET['page_access_token'];
$page_id = $_GET['page_id'];
echo $url;
echo $message;
$script = <<< JS
$(function() {
var form = document.querySelector('#form');
const data = Object.fromEntries(new FormData(form).entries());
console.log(data['postImage']);
var formData = {
"message": `$message`,
"url": "$url",
'access_token': "$page_access_token"
}
console.log(formData);
FB.api(
'/$page_id/photos',
'POST',
formData,
function(response) {
console.log(response);
}
)
});
JS;
return $script;
}
I have used PHP to get the file and store it on the server and then sent the request using FB.api() which is available through Facebook Graph API JavaScript SDK.