In this series of Facebook API tutorials, today we will continue with creating albums on Facebook Fan Page. The main meat of this tutorial is getting the right access token for your fan page. To get the right access token, follow these steps:
Other Facebook Tutorials:
How to upload photos to you Facebook Fan Page
Access Facebook Photo Albums using PHP - v1.0
Upload photos on Facebook using PHP
See it working
Live Demo Download Script
- Go to https://developers.facebook.com/tools/explorer?method=GET&path=me%2Faccounts
- Enter https://graph.facebook.com/me/accounts in the second text box
- Click on submit
- Json data will be returned
- From that select the access token of your Fan page.
Other Facebook Tutorials:
How to upload photos to you Facebook Fan Page
Access Facebook Photo Albums using PHP - v1.0
Upload photos on Facebook using PHP
See it working
Live Demo Download Script
<?php
require_once 'library/facebook.php';
$facebook = new Facebook(array(
'appId' => '<app_id>',
'secret' => '<app_sercret>',
'fileUpload' => true
));
?>
<html>
<head>
<title>WebSpeaks.in | Create album and upload photos to Facebook Fan Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
//It can be found at https://developers.facebook.com/tools/access_token/
$access_token = '<you_access_token>';
$params = array('access_token' => $access_token);
//The id of the fanpage
$fanpage = '<your_fan_page_id>';
/*
* Go to https://developers.facebook.com/tools/explorer?method=GET&path=me%2Faccounts
* Enter https://graph.facebook.com/me/accounts in the second text box
* Click on submit
* Json data will be returned
* From that select the access token of your Fan page
*/
$page_access_token = '<your_page_access_token>';
$facebook->setAccessToken($page_access_token);
$facebook->setFileUploadSupport(true);
//Create an album
$album_details = array(
'message'=> 'Test album',
'name'=> 'Album name'.date('Y-m-d H:i:s') //should be unique each time
);
$album = $facebook->api('/'.$fanpage.'/albums', 'post', $album_details);
//The id of the album
$album_id =$album['id'];
//Replace arvind07 with your Facebook ID
$accounts = $facebook->api('/arvind07/accounts', 'GET', $params);
foreach($accounts['data'] as $account) {
if( $account['id'] == $fanpage || $account['name'] == $fanpage ){
$fanpage_token = $account['access_token'];
}
}
$valid_files = array('image/jpeg', 'image/png', 'image/gif');
if(isset($_FILES) && !empty($_FILES)){
if( !in_array($_FILES['pic']['type'], $valid_files ) ){
echo 'Only jpg, png and gif image types are supported!';
}else{
#Upload photo here
$img = realpath($_FILES["pic"]["tmp_name"]);
$args = array(
'message' => 'This photo was uploaded via WebSpeaks.in',
'image' => '@' . $img,
'aid' => $album_id,
'no_story' => 0,
'access_token' => $fanpage_token
);
$photo = $facebook->api($album_id . '/photos', 'post', $args);
if( is_array( $photo ) && !empty( $photo['id'] ) ){
echo '<p><a target="_blank" href="http://www.facebook.com/photo.php?fbid='.$photo['id'].'">Click here to watch this photo on Facebook.</a></p>';
}
}
}
?>
<!-- Form for uploading the photo -->
<div class="main">
<p>Select a photo to upload on Facebook Fan Page</p>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
<p>Select the image: <input type="file" name="pic" /></p>
<p><input class="post_but" type="submit" value="Create album and Upload" /></p>
</form>
</div>
</body>
</html>
I love you... this is the best article written in much required detail. Please write more - a live saver. One thing - do you have any idea how I dynamically retrieve my administrator's token?
ReplyDeletelife saver even..
ReplyDeleteI received this error
ReplyDeleteFatal error: Call to undefined method Facebook::setAccessToken() in /web/htdocs/www.salernitana.org/home/fbupload2.php on line 32
hi in your live demo it says token expired... and i tried your other article too...
ReplyDeletei guess you are making some mistake while feeding in the token.
actually by researching on net i found out that facebook tokens automatically expire after 1 hour.
so you need to write code to grab a new access token each time and i tried that for 1 whole day but i was not successful to code that can you help me with that?
Thank You..
hi in your live demo it says token expired... and i tried your other article too...
ReplyDeletei guess you are making some mistake while feeding in the token.
actually by researching on net i found out that facebook tokens automatically expire after 1 hour.
so you need to write code to grab a new access token each time and i tried that for 1 whole day but i was not successful to code that can you help me with that?
Thank You..
@Dhirendra, Thanks for notice. Demo has been fixed now.
ReplyDeleteI tested this and it works great.
ReplyDeleteIm trying to upload from a form with the javascript FB.api.
Do you know how to do that?
I can upload if the image source is a image on my server but not if I get it from the form field.
Do you have any inputs on what I need to think about when uploading from a form?
Thanks a lot!.
Regards Claes
Hi
ReplyDeleteThe live demo doesnt display anything. Is this just me? It just displays a blank page.
I have been struggling with this myself and see the same problem with the live demo.
Tested in chrome and firefox. Same problem.
Thanks
@Josh, The live demo has been fixed. Te token needs to be updated after some time intervals.
ReplyDeletethe live demo get black page, I do tried the code by my self, and the code return album and image id, but I cannot find the albom or the image.
ReplyDeletealways get from facebook
"This content is currently unavailable
The page you requested cannot be displayed at the moment. It may be temporarily unavailable, the link you clicked on may have expired, or you may not have permission to view this page."
Please. help me.
ReplyDeleteDid facebook change the process?
https://developers.facebook.com/tools/explorer?method=GET&path=me%2Faccounts returns no token on the json.
Hello to every body, it's my first visit of this blog; this blog includes remarkable and really fine stuff for readers.
ReplyDeleteAlso visit my web page: achat vues Youtube
Everyone loves what you guys are usually up too.
ReplyDeleteSuch clever work and reporting! Keep up the great works guys I've included you guys to my personal blogroll.
my web page ... plus de followers
I am curious to find out what blog system you are using?
ReplyDeleteI'm having some minor security problems with my latest site and I would like to find something more risk-free. Do you have any suggestions?
Also visit my web site - acheter followers
Have you ever considered publishing an ebook or guest authoring on other sites?
ReplyDeleteI have a blog centered on the same information you discuss and would love to have you share some stories/information.
I know my readers would enjoy your work.
If you are even remotely interested, feel free to send me
an e mail.
Also visit my page :: comment augmenter vues youtube