Facebook graph API: Upload page image - facebook

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);

Related

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

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

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

facebook post image to user wall and image store in my fan page album

Isn't possible facebook allow us from my apps post or share image to user wall and then at the same time the user image will store in my fan page album?
And suggestion and opinion how to do it? hopefully you guy able provide some code for me
step1
$user = $facebook->getUser();
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => 'email,publish_stream,user_birthday,user_location,user_work_history,user_about_me,user_hometown,user_photos'
)
);
step2 , 4 ,5
$file='images/'.$_FILES["file"]['name'];
if( move_uploaded_file($_FILES["file"]["tmp_name"],$file))
{
$facebook->setFileUploadSupport(true);
$post_data = array(
'name'=>$_POST['album'],
'description'=>$_POST['album']
);
$data['album'] = $facebook->api("/me/albums", 'post', $post_data);
//$file = $file_name;
$post_data = array(
"message" => $_POST['message'],
"source" => '#' . realpath($file)
);
$album_id = $data['album']['id'];
$data['photo'] = $facebook->api("/$album_id/photos", 'post', $post_data);
}
it is possible...steps
take user publish stream permissions - scope - publish_stream,user_photos
set application for uploading image..setFileUploadSupport(true);
send the image to users wall using feed api
fetch user photo
Upload it on pages' album by albums api

Facebook Posting a url to page

I use below code to post an url to facebook page as a admin but if i post an message,it is posting but it is not posting photos and url.
$args = array(
'access_token' => token,
'message' => $url
);
$wall = $facebook->api("/".$pid."/feed","post",$args);
Is there any other function that does that?
You need to add
'caption'
'link'
'picture'
variables to your array to show a picture with a caption and a link to a url.

Create Facebook event as Page with rGraph API

I am trying to create an event on Facebook on behalf of the page i own with Graph API.
From FB documentation i read that it is done like this:
POSTing name and start_time (other things are optional) to
https://graph.facebook.com/{Page_ID}/events
with proper premissions (create_event, manage_pages)
playing around with this in Graph API Explorer i do get event created but it is done under my own profile rather than Page.
So is it possible to create the event under the Page i own rather than my profile?
PS i double checked the page id so it is not my profile id
PPS I know there are allready many discussions about this here, but unfortunatley i couldnt find a straightforward answer to my question.
So i figured this out myself. I needed to post it with page access token. heres the code
$page_info = $this->fb->api('/' . $page_id . '?fields=access_token');
if (!empty($page_info['access_token'])) {
$path = realpath(APPPATH . '../path/to/img');
$args = array(
'access_token' => $page_info['access_token'],
'start_time' => $start_time,
'description' => $description
'name' => $name,
'location' => $loaction
);
$img = 'img.jpg';
if ($upload) {// if picture is needed
$args['#' . $img] = '#' . $path . '/' . $img;
$this->fb->setFileUploadSupport(true);
}
$res = $this->fb->api("/" . $this->input->post('fb_event'), "post", $args); // should give back event id
}