0

I am an admin of Facebook fan page where i need to post an image as admin. While i use this code it takes posts in my own Facebook wall. I have the manage permission for the app. Any idea?

<?php
    $album_id ='9543045';
    $access_token='W4997qoxbdFGLZA1d8a7JhoNoKxZBcNdFcdUXgDXa7k7oeFvKBIZA6OHf81pZAyV5nCAZD';
    $file= '/Users/bazinga/Desktop/banner1.png';
    $args = array(
       'message' => urlencode('Hello'),
        'description' => urlencode('World'),
        'access_token' => urlencode($access_token)
    );
    $args[basename($file)] = '@' . realpath($file);
    $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);
    $data = curl_exec($ch);
    //returns the photo id
    print_r(json_decode($data,true));
Ramesh
  • 2,295
  • 5
  • 35
  • 64

1 Answers1

0

You require Page Access Token to upload photo on behalf of page admin.

You can obtain your page access token via https://graph.facebook.com/me/accounts?access_token=USER_ACCESS_TOKEN

An alternative way, if you already know the page id, then you can just request

https://graph.facebook.com/PAGE_ID?fields=access_token&access_token=USER_ACCESS_TOKEN

Also, make sure you request status_update premission, as answered by me at why does posting to facebook page yield "user hasn't authorized the application"

The last thing, i think description and access_token is redundant in your $args array.

Community
  • 1
  • 1
林果皞
  • 7,539
  • 3
  • 55
  • 70