How to create an Photo Album in Facebook Using API - php

How to create like this ?
Users from my website can Upload image to their FB page.
for example : myfbcover.com
I uploaded these files (https://github.com/facebook/php-sdk) .
Now what to Next ?
Please help me !

Basic example:
$facebook = new Facebook(array(
'appId' => 'APP_ID',
'secret' => 'APP_SECRET',
'cookie' => true,
));
$user = $facebook->getUser();
if ($user) {
$facebook->setFileUploadSupport(true);
//Create an album
$album_details = array(
'message'=> 'Album desc',
'name'=> 'Album name'
);
$create_album = $facebook->api('/me/albums', 'post', $album_details);
//Get album ID of the album you've just created
$album_uid = $create_album['id'];
//Upload a photo to album of ID...
$photo_details = array(
'message'=> 'Photo message'
);
$file='app.jpg'; //Example image file
$photo_details['image'] = '#' . realpath($file);
$upload_photo = $facebook->api('/'.$album_uid.'/photos', 'post', $photo_details);
}
This is just an exmaple. You could find lots of sources on googling.
Hope it helps you get started

Related

Post to facebook fan page as a user

Can anyone tell me how to do this using php sdk.
I have using this code. This code was suppose to post to user's timeline as well as the fan page. The person who is logged in would be the one who is doing the post. So on the fan page
this would have appeared under recent activity. However, now the code has stopped working saying that the user has not authorised the app. I do not understand why this could be happening.
include("../php-sdk/facebook.php");
/*START FACEBOOK lOGIN*/
$facebook = new Facebook(array(
'appId' => Appid,
'secret' => Appsecret,
'cookie' => true
));
$pageId = PageId;
$user= $facebook->getUser();
$newfbuser = 0;
if (!empty($user)) {
$uid = $facebook->getUser();
// $fb_access_token = $facebook->getAccessToken();
$url = $facebook->getLoginUrl(array(
'req_perms' => 'email,status_update,publish_stream, manage_pages'
));
$user = $facebook->api('/me');
$fb_access_token = $facebook->getAccessToken();
$param = array(
'method' => 'users.getInfo',
'uids' => $uid,
'fields' => array('name','sex')
);
$users_getinfo = $facebook->api($param);
$save['oauth_uid'] = $users_getinfo['0']['uid'];
$save['oauth_provider'] = 'facebook';
$save['facebook_email'] = $users_getinfo['0']['email'];
$save['name'] = $users_getinfo['0']['name'];
$link_url = "http://google.com";
$pic = 'http://xxxxx/images/Testing.jpg';
$attachment = array(
'access_token' =>$fb_access_token,
'message' => 'This is a message by bob',
'link' => 'http://xxxxx/');
// print_r($fb_access_token);
echo 'on my timeline<br />';
$facebook->api("/me/feed",'post',$attachment);
$facebook->api("/$pageId/feed",'post',$attachment);
echo 'successfully posted';
}
// $fb_access_token = $facebook->getAccessToken();
$url = $facebook->getLoginUrl(array(
'req_perms' => 'email,status_update,publish_stream, manage'
));
No such thing call "manage", the correct permission was manage_pages

How to upload image to existing fan page if the user is not the administrator fan page?

This code return "invalid album id".
if the user is admin fan page then it works.
But I need that all users can upload photos through the app in fan page album.
$config = array(
'appId' => '553412241404428',
'secret' => '...',
'fileUpload' => true, // optional
'allowSignedRequest' => false, // optional, but should be set to false for non-canvas apps
'cookie' => true
);
// exist ALBUM from URL
$ALBUM_ID = "765349256813004";
$filename = $_GET['file'];
$facebook = new Facebook($config);
$facebook->setFileUploadSupport(true);
if(!$user = $facebook->getUser()) {
echo "<a target='_blank' href='".$facebook->getLoginUrl(array('scope' => 'publish_actions,publish_stream,user_photos,email,read_stream,manage_pages,user_groups'))."'>Auth</a>";
exit;
}
$access_token = $facebook->getAccessToken();
$FILE_PATH = $_SERVER['DOCUMENT_ROOT']."facebook/uploads/".$filename;
$args = array('message' => 'Photo Caption');
$args['image'] = '#' . realpath($FILE_PATH);
$args['access_token'] = $access_token;
$data = $facebook->api('/'. $ALBUM_ID . '/photos', 'post', $args);
print_r($data);exit;

Upload Facebook photos on page timeline (PHP)

I have a cache of pictures in my website, and I need to upload these photos to my Facebook fan page 4-5 times a day. My code creates an album and also uploads the photos to the album, however, these photos do not appear in the timeline.
So my question is, how do I upload photos to my fan page such that they appear on the timeline, on the wall. The language in use is PHP
Any help is greatly appreciated.
Thanks
Edit 1:
Here is the code:
<?php
require 'facebook.php';
$facebook = new Facebook(array(
'appId' => "AAAAAAAAAA",
'secret' => "BBBBBBBBBB",
));
$fanpage_token = "ZZZZZZZZZZZZZZZZZZZZZZZZZZ";
$facebook->setFileUploadSupport(true);
//Create an album
$album_details = array(
'message'=> 'test album',
'name'=> 'album'
);
$create_album = $facebook->api('/PAGE_ID/albums', 'post', $album_details);
$album_uid = $create_album['id'];
echo $album_uid;
$img = '7newx.jpg';
$args = array(
'message' => 'Random message',
'image' => '#' . $img,
'aid' => $album_uid,
'no_story' => 1,
'access_token' => $fanpage_token
);
$photo = $facebook->api($album_uid . '/photos', 'post', $args);
?>
Ok, so I guess you tried removing the no_story and found that it didn't affect the timeline. Thats what I found too.
What you have to do is make another post, but this time, as a link. You need to use your array $photo, which was returned from your first post. If you examine that you should see it has a single element called ['id'], containing the id of your uploaded photo.
You need to turn that into a link that facebook would use to display. This is simple.
if (isset($photo['id']))
{
$message = "My latest photo";
$link = "https://www.facebook.com/photo.php?fbid=".$photo['id'];
$attachment = array
(
'access_token'=>$fanPageAccessToken,
'type' => 'photo',
'message' => $message,
'link' => $link
);
$result = $facebook->api($fanPageId.'/links/','post',$attachment);
}
I am assuming you can work out what $fanPageAccessToken and $fanPageId are.
If you do that, you should get what you want.

Facebook PHP SDK app to upload photo to page

I am trying to create a Facebook application that uploads images to a page. I can manage to upload photos to the user's photo album but not to the page's.
Here's my code:
require_once('php-sdk/facebook.php');
$config = array(
'appId' => 'XXXXXXXX',
'secret' => 'YYYYYYYYYYYYYYYY',
'fileUpload' => true,
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
$photo = $_POST['image'];
$message = "Lorem ipsum";
echo $user_id;
if ($user_id) {
$ret_obj = $facebook->api('/XXXXXXXXXX/photos', 'POST', array(
'access_token' => $access_token,
'source' => '#' . $photo,
'message' => $message
)
);
} catch(FacebookApiException $e) {
$login_url = $facebook->getLoginUrl( array(
'scope' => 'user_photos,publish_stream,photo_upload'
));
echo 'Please login.';
print_r($e);
error_log($e->getType());
error_log($e->getMessage());
}
echo '<br />logout';
} else {
$login_url = $facebook->getLoginUrl( array( 'scope' =>user_photos,publish_stream,photo_upload,manage_pages' ) );
echo 'Please login.';
}
Any help would be much appreciated.
When you do:
$facebook->api('/XXXXXXXXXX/photos', 'POST', array(
... is XXXXXXXXXX your appId or the albumId? You must do the call to your album ID.
Also make sure you are using the correct $access_token. To make sure look at:
https://graph.facebook.com/yourPageID?fields=access_token&access_token=YourPersonalAccessToken
where yourPersonalAccessToken is:
A Page admin access_token for this page; The current user must be an
administrator of this page; only returned if specifically requested
via the fields URL parameter.
Your must grant to graph api manage_pages permissions.

Upload image from facebook app

I'm trying to upload image from a Facebook app. I get the following error: Fatal error: Uncaught CurlException: 26: failed creating formpost data thrown in /storage/content/00/103300/gjutveckling.se/public_html/fb/src/base_facebook.php on line 886
This is my code. The strange thing is that i got it to work, but then all if the sudden it stopped working. Can this be a SSL related problem, I just got a new one? Any suggestions must appreciated!
<?php
$facebook = new Facebook(array(
'appId' => $appid,
'secret' => $appsecret,
'fileUpload' => true,
'cookie' => true,));
$facebook->setFileUploadSupport(true);
$FILE_PATH = 'imdage_' . $user_id . '.jpg';
$args = array('message' => 'message');
$args['image'] = '#' . realpath($FILE_PATH);
$data = $facebook->api('/me/photos', 'post', $args);
?>
I tried this aswell, it doesn't give me an error, but no photo is uploaded.
<?php
function upload_to_facebook($image_data, $access_token)
{
$facebook = new Facebook(array(
'appId' => 'xxx',
'secret' => 'xxx',
));
$facebook->setFileUploadSupport(true);
$albums = $facebook->api('/me/albums', 'get', array('access_token' => $access_token));
foreach ($albums[data] as $album)
{
if ($album['name'] == $image_data['album_name'])
{
$album_id = $album['id'];
}
}
if (!$album_id)
{
//Create an album
$album_details = array(
'message' => 'Album by FB APP',
'access_token' => $access_token,
'name' => $image_data['album_name']
);
$create_album = $facebook->api('/me/albums', 'post', $album_details);
//Get album ID of the album you’ve just created
$album_id = $create_album['id'];
}
//Upload a photo to album of ID…
$photo_details = array(
'message' => 'test image',
'access_token' => $access_token,
);
$photo_details['skrivreglerny.png'] = '#' . realpath($image_data['file']);
$upload_photo = $facebook->api('/'.$album_id.'/photos', 'post', $photo_details);
return $upload_photo;
}
?>
Try this please
$photo_details = array(
'message' => 'message',
'access_token' => 'USER ACCESS TOKEN',
);
$photo_details['image'] = '#'.realpath($image_data['file']);
$upload_photo = $facebook->api(('/me/photos', 'post', $photo_details);
see this link
http://pramodsivadas.co.in/2011/11/upload-an-image-to-a-facebook-album-facebook-php-sdk/

Categories