1
votes

I made this small app that basically asks a user to login and post a photo to a FB page. the code I have works great for posting on your own wall, but when it comes to posting to a page I am having some difficulties.

require_once('../src/facebook.php');

$config = array(
'appId' => 'XXXXXXXXXXXXX',
'secret' => 'XXXXXXXXXXXXXXX',
'fileUpload' => true,
);

$facebook = new Facebook($config);
$user_id = $facebook->getUser();

$photo = realpath("mypic.png"); // Path to the photo on the local filesystem
$message = 'Photo upload via the PHP SDK!';

if($user_id) {
try {
$ret_obj = $facebook->api('/PAGE_ID_HERE_??/photos', 'POST', array(
'source' => '@' . $photo,
'message' => $message,));

if i use feed instead of photos, like here

$facebook->api('/PAGE_ID_HERE_??/FEED',

it works, but only posts the message. i have all permissions needed:
user_photos user_videos publish_action
manage_pages publish_stream

1

1 Answers

1
votes

To interact with the page you need the access token of the user. The user needs to be the 'manager' or 'content creator' of the page. And those tokens are per user per page.

I have a PHP example where this is handled using a cURL call. I think it gives the idea.

$ch = curl_init();
$url = "https://graph.facebook.com/" . $album_id . "/photos?access_token=" . $access_token;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$retdata = curl_exec($ch);
echo($retdata);

In this one the $album_id is the album id of the page and the $access_token is the access token for the user for the page.

Also here's an example by Facebook for a personal album. You can chage the album ID and the token and use that approach.