Using album image as thumbnail for open graph story - php

I am working on a sample application in PHP to upload a picture to facebook and use that link to create a story. All is well except for the thumbnail is not showing up. Does anyone know if it is restricted to actually use the uploaded album image as a thumbnail?Below is the code i am using
$ret_obj = $facebook->api('/me/photos', 'POST', array(
'image' => '#'.realpath($photo),
'message' => $message,));
$imgId = $ret_obj['id'];
$objectjson = json_encode(array('title'=>'Sample','url'=>'http://facebook.com/photo.php?fbid='.$imgId,'description'=>'This is a sample','image'=>'http://facebook.com/photo.php?fbid='.$imgId));
$ret_obj = $facebook->api('/me/objects/xxxx:yyyy', 'POST', array(
'object' => $objectjson));
EDIT: Forgot to mention that i am using the suggested size image and any other images other than the uploaded ones can be used as thumbnail

Related

Facebook API: Upload photo becomes album instead of single post

I'm working with facebook API that enable user to upload a photo and give caption(like instagram).
Here is my code to upload photo:
$request = (new FacebookRequest( $session, 'POST', '/me/photos', array(
'message' => $caption,
'url' => $img_url
) ) )->execute();
$result = json_decode($request->getRawResponse(), true);
If I uploaded more than one photo in the close time, the photos will be grouped in an album instead of a single post.
And facebook will return previous post id, instead of facebook post's id that I have just uploaded.
Is it normal behaviour? Does anyone know how to fix the photo id?

How do I can post a multiple photos via Facebook API

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

Posting an image from one server to another? Facebook PHP Sdk

So, I have an image on a server # a location like this.... http://www.theserver.com/image/img.jpg
I have a Facebook connected website running here http://www.theOTHERserver.com/facebook/.
On the other server I plan to add some code like so..
$photo = "http://www.theserver.com/image/img.jpg";
$ret_obj = $facebook->api('/me/photos', 'POST', array(
'source' => '#' . $photo,
'message' => $message,
)
);
Does anyone know if this (the above) is going to work? There are of course many examples where the photo is stored locally...e.g.
$photo = "/img.jpg";

How to upload a photo to a specific album and if not exist create that album?

i have a app that upload a photo to users profile
but when it upload a photo it goes to a new album named with a app name.
what i want to do is upload photo to a album named by me like
"my test album" or some thing.
and when the album my test album doesn't exist it should create a album "my test album".
First you want to check to see if the album you are creating exists, otherwise Facebook will keep generating the same album with the same name.
$album = $facebook->api($param);
if (!$album) {
$album_details = array(
'message'=> 'Your album description goes here',
'name'=> $albumName
);
$create_album = $facebook->api('/me/albums', 'post', $album_details);
$album_uid = $create_album['id'];
} else {
$album_uid = $album[0]['object_id'];
}
Once you've got the album id, you can upload your photo like so:
$photo_details = array(
'message'=> 'Photo Description Goes Here',
'image'=> '#' . realpath($theUploadFile)
);
$upload_photo = $facebook->api('/'.$album_uid.'/photos', 'post', $photo_details);
I wrote a good tutorial that has this feature in it among many others. My photo upload features is built in with a multi-select and allows you to upload photos and videos at the same time. You can view the entire tutorial and download the package here: http://www.epixseo.com/index.php/facebook-php-3-3-1-and-javascript-sdk-graph-api-tutorial/
On first upload you can create the album :
$facebook->api('/me/albums','post',$album_details);
and save the album_id that is returned. Then for sequential uploads you can make calls to :
$facebook->api('/'.$album_id.'/photos','post',$photo_details);
It is all covered in this facebook developers blog post.

post photo to friend wall through graph api

I'm trying to post a photo to one of friends wall using the new graph api. For this I have the following code:
$attachment = array(
'message' => 'Posted a photo',
'source' => '#' . realpath(PATH_TO_MY_PHOTO)
);
$facebook->setFileUploadSupport(true);
$facebook->api('/'.$id_friend.'/feed?access_token='.$access_token, 'post', $attachment);
The problem is that the picture is not uploaded. I mean, it only post the message without any picture. This does not work either if I try to post on the current user wall.
Does anyone have any idea on how to achieve this ? Thanks.
p.s. I'm requesting only publish_stream permission
In order to post a poto to a users wall, as in publishing a photo and not a stream story,
you can post to here:
http://graph.facebook.com/ALBUM_ID/photos.
for more information visit facebook's graph api photo documentation.
You should replace "source" with "picture".
$imagepath='http://site.com/pic.jpg';
$attachment = array(
'message' => 'Posted a photo',
'picture' => $imagepath
);
$facebook->setFileUploadSupport(true);
$facebook->api('/'.$id_friend.'/feed?access_token='.$access_token, 'post', $attachment)
You can optionally add some more fields. for more details:
https://developers.facebook.com/docs/reference/api/post/

Categories