How to create post with multiple images on fb api? [duplicate] - facebook

Now I posting a single photo to wall like this:
$response = $facebook->api("/$group_id/photos", "POST", array(
'access_token=' => $access_token,
'message' => 'This is a test message',
'url' => 'http://d24w6bsrhbeh9d.cloudfront.net/photo/agydwb6_460s.jpg',
)
);
It works fine, but can I somehow post a multiple photos, something like this:

You can now publish multiple images in a single post to your feed or page:
For each photo in the story, upload it unpublished using the {user-id}/photos endpoint with the argument published=false.
You'll get an ID for each photo you upload like this:
{
"id": "10153677042736789"
}
Publish a multi-photo story using the {user-id}/feed endpoint and using the ids returned by uploading a photo
$response = $facebook->api("/me/feed", 'POST',
array(
'access_token=' => $access_token,
'message' => 'Testing multi-photo post!',
'attached_media[0]' => '{"media_fbid":"1002088839996"}',
'attached_media[1]' => '{"media_fbid":"1002088840149"}'
)
);
Source: Publishing a multi-photo story

You can make batch requests as mentioned here: https://stackoverflow.com/a/11025457/1343690
But its simple to loop through your images and publish them directly.
foreach($photos as $photo)
{
//publish photo
}
Edit: (regarding grouping of photos on wall)
This grouping is done by facebook automatically if some photos are uploaded into the same album.
Currently you cannot create an album in a group via Graph API - it is not supported (as of now), see this bug.
But you can do this - create an album manually, then get the album_id by-
\GET /{group-id}/albums, then use the the code with album_id instead of group_id-
foreach($photos as $photo){
$facebook->api("/{album-id}/photos", "POST", array(
'access_token=' => $access_token,
'name' => 'This is a test message',
'url' => $photo
)
);
}
I've tested it, see the result-

Actually you can upload a multi story photo(I did it using Graph Api and PHP) but the problem comes if you need scheduled this post.Your post is schedule but also it shows on the page's feed.
P.S. I'm using Graph Api v2.9
PHP Code
$endpoint = "/".$page_id."/photos";
foreach ($multiple_photos as $file_url):
array_push($photos, $fb->request('POST',$endpoint,['url' =>$file_url,'published' => FALSE,]));
endforeach;
$uploaded_photos = $fb->sendBatchRequest($photos, $page_access_token);
foreach ($uploaded_photos as $photo):
array_push($data_post['attached_media'], '{"media_fbid":"'.$photo->getDecodedBody()['id'].'"}');
endforeach;
$data_post['message'] = $linkData['caption'];
$data_post['published'] = FALSE;
$data_post['scheduled_publish_time'] = $scheduled_publish_time;
$response = $fb->sendRequest('POST', "/".$page_id."/feed", $data_post, $page_access_token);
$post_id = $cresponse->getGraphNode()['id'];

You will need to upload each photo first with published state to false, and then use the ID's of the unpublished photos to the /me/feed endpoint to schedule the photo. The schedule needs to be within the 24 hours from the time the photos are uploaded as facebook deletes all unpublished photos in 24 hours.
Ref:
https://developers.facebook.com/docs/graph-api/photo-uploads/

There is no way to publish more than one photo in the same graph API call.
See documentation: https://developers.facebook.com/docs/graph-api/reference/user/photos

Related

Facebook app post on user's wall low quality image

So I have app, which post image on user's wall. Problem is that, It automatically choose App's image (Page Tab Image 111x74) and add It on user's wall (exapanded). I need to add custom image, not app's page tab image. For now my code:
$photoCaption = 'Text......';
$link = ('https://www.facebook.com/pages/mypage/1555555555?sk=app_1800000000');
# Post Data for Photos API
$post_data = array(
'message' => $photoCaption,
'link' => $link,
'caption' => 'This is captions!'
);
$apiResponse = $facebook->api('/me/feed', 'POST', $post_data);
You post photos with /me/photos, not /me/feed, hereĀ“s the information you need including example code:
https://developers.facebook.com/docs/graph-api/reference/v2.1/user/photos

Post to facebook page from facebook page tab app

I want to post activities from page tab app to my own facebook page. I referred many documents to post message to app.
My php code :
$GLOBALS["facebook"] = new \Facebook_Facebook(array('appId' => $facebook_app_id, 'secret' => $facebook_secret,));
$page_info = $GLOBALS["facebook"]->api("/pageid?fields=access_token");
print_r($page_info);die;
if (!empty($page_info['access_token'])) {
$args = array(
'access_token' => $page_info['access_token'],
'message' => 'TEST'
);
$postId = $facebook->api("/pageid/feed", "post", $args);
But pageinfo variable has only id. I'm not getting access_token. Any idea what else need to be done?
We need manage_pages permission which we can get it using https://www.facebook.com/dialog/oauth?client_id=client_app_id&client_secret=9ea3e2eff7c65b1fcf1a633da&redirect_uri=YOUR_REDIRECT_URL&scope=read_stream,publish_stream,offline_access,manage_pages.
After that using above code we will get access token which can be used to publish to our page

Facebook stream publish to wall as App/Page

i wonder if it is possible to post to a users stream/wall by using stream_publish as a page or an app. Currently I've granted my app to stream_publish on my own wall. But when I post to my stream from the app the post is displayed like I would've posted it by myself instead of having the app posted it to my wall.
I want to have the post looks like the app posted it. I already used the uid-parameter in the stream_publish params and set it to the appId ... but the post still looks like an own post.
my code:
$postData = array( $feed_dir = '/'.$fb_uid.'/feed/',
$method = 'POST',
$msg_body = array( 'access_token' => $arrUserData['access_token'],
'message' => 'here goes the message',
'name' => '-name-',
'link' => 'http://some-url',
'description' => '-description-',
'actions' => ($actions),
'uid' => $iniHandler->getIniSetting('facebook.app.id')
)
);
try {
$res = $facebook->api($feed_dir, $method, $msg_body);
}
catch (Exception $e) {
$err_str = $e->getMessage();
}
Or is ther another way to post to a wall and let the post looks like a post by an app? My users can sign up for issues in my app and my app posts some news to the users wall ... thats my aim.
You also need manage_pages permissions. Once you have those, HTTP Get me/accounts and that will be a list of pages that the logged in user is admin of. In that list will be a unique page access_token. Use that token to http post a new wall/stream item.

Facebook graph API: Upload page image

Is it possible to upload an image to a facebook page as the page picture (not a photo album picture) through the graph api? And then is it possible to also define the offsets to create the miniature picture when a user writes posts as this page?
Yes below worked for me in many places
$file = '#' . realpath('path_to_image');
// Fill in your arguments.
$args = array(
'access_token' => $token,
'image' => $file,
'aid' => $albumid
);
// Send the api request.
$data = $facebook->api('/'.$pageid.'/photos', 'post', $args);

How to publish a wall post with 2 pictures

I'm creating a small app in which people compare diferent pictures of him and his friends.
The app is verry simple and I have no problem with it. The problem comes when users try to publish in their walls. I want the posts to be something like:
I know that by doing something like:
$parameters = array('message' => 'this is my message',
'name' => 'Name of the app',
'caption' => "Caption of the Post",
'link' => 'http://www.link.com',
'description' => 'description',
'picture' => 'http://mysite.com/pic.gif');
$result = $facebook->api('/user_id/feed/', 'post', $parameters );
You can post on a wall, but that limits the app to one picture only and I want the users to be able to post two diferent picures.
Is there a way to publish a post like the one in the image?
EDIT:
Added a picture that might explain what I want in a better way.
Without getting too deep into the Facebook API (since I've never seen a FB app post two photos, I'm guessing it's not possible - most of the apps I see would be as spammy as allowed...), I'd probably start by just compositing the two images (and doing any overlays I wanted to do) at the server side dynamically using something like PHP and the ImageMagick classes.
Then, just feed Facebook the URL to the PHP script that does the compositing, e.g.
$parameters = array('message' => 'this is my message',
'name' => 'Name of the app',
'caption' => "Caption of the Post",
'link' => 'http://www.link.com',
'description' => 'description',
'picture' => 'http://mysite.com/pic.php?pic1_id=1234&pic2_id=2345&favorite=pic1');
$result = $facebook->api('/user_id/feed/', 'post', $parameters );
Of course, I don't know what sort of dimensions Facebook will take for a picture to be posted with a wall post - it might not be wide enough for this to work. I'd try it, though...
Facebook did away with multiple pictures in a post a while ago. When you were able to do it, people were creating "banners" that consisted of multiple pictures side by side. Now only 1 pictured is displayed for any wall post. You can post more pictures, but they won't be sure by default. Facebook will add a "more" link, although they may have done away with that also.
At least in Javascript when using Facebook.streamPublish, as second argument you can give an attachment object that may contain an array of media. Here are the relevant pieces from my app Japan Name:
var media = [];
for (var i = 1; i < syls.length; i++) {
media.push({
'type' : 'image',
'src' : 'http://youarecu.appspot.com/chars/' + syls[i] + '.png',
'href' : streamhref
});
}
...
var attachment = {
'href':'http://apps.facebook.com/youarecute/?from_wall=true&postid=f'+uid+'{{postid}}',
'name':linkedtxt,
'description':txt,
'media':media
};
...
Facebook.streamPublish("", attachment, ...