Wednesday, 28 August 2013

Upload photo to facebook fan page wall

Upload photo to facebook fan page wall

This tutorial has helped me writing the following code; with it I am able
to upload a photo to an album on my fan page. How can I make it so that I
can publish to the fan page wall and not just upload the photo to the
album? I am looking in implementing the required code within this below.
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
require_once 'library/facebook.php';
$facebook = new Facebook(array(
'appId' => 'My APP ID',
'secret' => 'My Secret KEY',
'fileUpload' => true
));
#It can be found at https://developers.facebook.com/tools/access_token/
#Change to your token.
$access_token = 'My access token';
$params = array('access_token' => $access_token);
#The id of the fanpage
$fanpage = 'My Fan Page ID';
#The id of the album
$album_id ='My Album ID';
$accounts = $facebook->api('/My User Name/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{
$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>';
}
}
}
?>

No comments:

Post a Comment