简体   繁体   中英

Problem with posting to Facebook Page using PHP script as Page account

I am using the following PHP code to post to my page wall. it is okay I can post successfully as admin but I want to post to the wall using the page name (page account). how do I post using the page name or page id. I tried to find good resource to explain Facebook API function but i did not find good documentation for it.

<?


require '../src/facebook.php';

$app_id = "XXX";
$app_secret = "YYY";

$facebook = new Facebook(array(
 'appId' => $app_id,
 'secret' => $app_secret,
 'cookie' => true
));

$user = $facebook->getUser();
//echo $user;

if(($facebook->getUser())==0)
{
 header("Location:{$facebook->getLoginUrl(array('req_perms' => 'user_status,publish_stream,user_photos,offline_access'))}");
 exit;
}
else {
echo "i am connected";
}

$attachment = array('message' => 'this is my message',
                'name' => 'This is my demo Facebook application!',
                'caption' => "Caption of the Post",
                'link' => 'example.org',
                'description' => 'this is a description',
                'picture' => 'http://example.org/image.gif',
                'actions' => array(array('name' => 'Get Search',
                                  'link' => 'http://www.google.com'))
                );
$status = $facebook->api('/123456789/feed', 'POST', $attachment);   // my page id =123456789
?>

I found the solution to the problem. To post or to do any task as a page, you need an access token. Here is my final code after the update, I want to share it with you because I found many people having difficulty with getting the correct access code for a page.

    <?


require '../src/facebook.php';

$app_id = "XXX";
$app_secret = "YYY";

$facebook = new Facebook(array(
 'appId' => $app_id,
 'secret' => $app_secret,
 'cookie' => true
));

$user = $facebook->getUser();
//echo $user;

if(($facebook->getUser())==0)
{
 header("Location:{$facebook->getLoginUrl(array('req_perms' => 'user_status,publish_stream,user_photos,offline_access,manage_pages'))}");
 exit;
}
else {
$accounts_list = $facebook->api('/me/accounts');
echo "i am connected";
}

//to get the page access token to post as a page
foreach($accounts_list['data'] as $account){
      if($account['id'] == 123456789){      // my page id =123456789
        $access_token = $account['access_token'];
        echo "<p>Page Access Token: $access_token</p>";
        }
    }

//posting to the page wall
$attachment = array('message' => 'this is my message',
                'access_token'  => $access_token,
                'name' => 'This is my demo Facebook application!',
                'caption' => "Caption of the Post",
                'link' => 'example.org',
                'description' => 'this is a description',
                'picture' => 'http://example.org/image.gif',
                'actions' => array(array('name' => 'Get Search',
                                  'link' => 'http://www.google.com'))
                );
$status = $facebook->api('/123456789/feed', 'POST', $attachment);   // my page id =123456789
var_dump($status);
?>

My Facebook OAuth is a little rusty, but I believe this will help:

http://bugs.developers.facebook.net/show_bug.cgi?id=10005

It is the exact bug you are describing, but I have no way of testing that it corrects the issue specifically.

This should help: http://developers.facebook.com/docs/reference/api/page/#feed

It looks like you need the manage_pages permission.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM