Upload photos to Facebook Fan Page using PHP

After writing the article on Uploading photos to Facebook albums, I though it would be a good idea to tell my readers how to upload photos to your Facebook fan page. Uploading photos to the fan page is a bit different from uploading photos to your album. Here we need the access token of your Fan page. If you know the id of your Fan page, it can be found on runtime though. So here is the script to upload the pics.



Live Demo


PHP Code:

<html>
<head>
<title>WebSpeaks.in | Upload images to Facebook</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php
require_once 'library/facebook.php';
$facebook = new Facebook(array(
'appId' => $app_id,,
'secret' => $app_secret,
'fileUpload' => true
));


//It can be found at https://developers.facebook.com/tools/access_token/
$access_token = '<Your access token>';

$params = array('access_token' => $access_token);

//The id of the fanpage
$fanpage = '330299184793';

//The id of the album
$album_id ='10150418901414794';

//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' => 1,
'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="Upload to my album" /></p>
</form>
</div>
</body>
</html>

 

A bit of CSS:
html{
font-family
: "lucida grande",tahoma,verdana,arial,sans-serif;
}
.main
{
width
:400px;
margin
:auto;
border
:2px solid #0066CC;
color
:#3B5998;
padding
:20px;
font-size
: 11px;
-moz-border-radius
: 4px 4px 4px 4px;
border-radius
: 4px 4px 4px 4px;
-moz-box-shadow
: 1px 1px 0 #d5d5d5;
background
: none repeat scroll 0 0 #F2F2F2;
}
.text
{
color
: #777777;
border
: 1px solid #BDC7D8;
font-size
: 11px;
height
: 15px;
}
.post_but
{
background
: none repeat scroll 0 0 #EEEEEE;
border-color
: #999999 #999999 #888888;
border-style
: solid;
border-width
: 1px;
color
: #333333;
cursor
: pointer;
display
: inline-block;
font-size
: 11px;
font-weight
: bold;
padding
: 2px 6px;
text-align
: center;
text-decoration
: none;
}
a
{
color
:#3B5998;
}

posted @ 2012-02-10 23:54  Lux.Y  阅读(515)  评论(0编辑  收藏  举报