Saturday, 31 August 2013

Upload a photo to a fan page of Facebook

Upload a photo to a fan page of Facebook

I am trying to upload a photo through my application with Facebook Graph
API to timeline of a Facebook's page. The problem is that it works only
when I publish the photo to my personal profile. The code I use is:
<?php
require 'includes/php-sdk/facebook.php';
define('APP_ID', '___________');
define('APP_SECRET', '___________');
define('REDIRECT_URI', '___________');
define('FAN_PAGE_ID', '___________');
define('SCOPE','publish_stream,user_photos,friends_photos, manage_pages');
$facebook = new Facebook(array(
'appId' => APP_ID,
'secret' => APP_SECRET,
'fileUpload' => true
));
//Get User ID
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if ($user)
{
$logoutUrl = $facebook->getLogoutUrl($params = array('next' =>
REDIRECT_URI));
}
else
{
$loginUrl = $facebook->getLoginUrl($params = array('scope' => SCOPE));
if ($_SERVER['SCRIPT_NAME'] != 'index.php')
header('Location:' . $loginUrl);
}
try
{
//$facebook->setFileUploadSupport(true);
$response = $facebook->api(
'/'.FAN_PAGE_ID.'/photos/',
'post',
array(
'message' => 'test',
'source' => '@'.realpath('image.jpg')
));
}
catch (FacebookApiException $e)
{
echo 'Could not post image to Facebook.';
}

No comments:

Post a Comment