使用PHP作为页面管理员发布到Facebook页面

问题描述:

Firstly I am aware that there are a million questions similar to this, but they are all either out of date (Facebook has changed and the instructions no longer work) or don't explain how to do specifically what I am asking.

I'm trying to register an app on Facebook so that I can autopost to the company Facebook page, of which I am an admin.

I'm trying to do this via PHP, with which I have considerable experience (PHP, not Facebook API.)

So far I have registered as a Facebook developer, made a Facebook app, got the appID and secret word, and downloaded the facebook-php-sdk from Github. I have attempted to follow a couple of tutorials but the Facebook developer/app pages have all changed and so the intructions are now invalid.

All I want to do is to be able to post automatically to my page's wall from the server via PHP, as if I posted the status update myself as the page admin. I don't understand how or why this is so difficult.

The Facebook app page has a million settings that I've never heard of and don't seem to be related, then there is no information that gives any direction to do what I want to do.

This is about as far as I've got and I've hit a wall. No idea what to do next. Facebook keeps asking me "Select how your app integrates with Facebook" but their options don't appear to include what I want, which is just to post on my own page. I don't appear to actually be able to use the app yet, as there are various settings its insisting on, like "Canvas URL", which I do not understand, etc. and then I obviously need to set permissions, yet I see no way to do this either.

What do I do?

首先我知道有一百万个类似的问题,但它们都已过时(Facebook 已经改变,说明不再有效)或者没有解释如何具体做我要问的事情。 p>

我正在尝试在Facebook注册一个应用程序,以便我可以自动提交到 公司的Facebook页面,其中我是管理员。 p>

我正在尝试通过PHP来做到这一点,我有相当丰富的经验(PHP,而不是Facebook API。) p >

到目前为止,我已注册为Facebook开发人员,制作了Facebook应用程序,获得了appID和密码,并从Github下载了facebook-php-sdk。 我试图遵循几个教程,但Facebook开发者/应用页面都已更改,因此说明文件现在无效。 p>

我想做的就是能够自动发布 通过PHP从服务器到我的页面墙,好像我自己发布状态更新作为页面管理员。 我不明白这是如何或为何如此困难。 p>

Facebook应用程序页面有一百万个我从未听说过但似乎没有相关的设置,那里 没有任何信息可以指示我做我想做的事情。 p>

这就是我所拥有的,而且我已经碰壁了。 不知道接下来该做什么。 Facebook一直在问我“选择你的应用程序如何与Facebook集成”,但他们的选项似乎没有包含我想要的内容,这只是在我自己的页面上发布。 我似乎还没有真正能够使用该应用程序,因为它坚持使用各种设置,如“Canvas URL”,我不明白等等,然后我显然需要设置权限,但我看到了 也无法做到这一点。 p>

我该怎么办? p> div>

Setting up an app

You are going to need to authenticate the user who has at least content creator rights on your page. So you need to choose 'Website with Facebook Login' and enter your website url. You'll also have to enter the domain (website url without protocol) Keep it in sandbox mode while you test it you can edit that later.

You don't really have to worry about other settings as the permissions to ask can be added directly in your php code.

"Online" Access

To logging and post directly to facebook you'll need to retreive an access token

Getting an access token

Here is a basic run down:

  • Get user to login and allow app with appropriate permissions (manage_pages, publish_stream) if he hasn't
  • Retreive user access token
  • Query /me/accounts with user access token to get the page id & access token

Then all you have to do is make your API call with id & access token to post on facebook

"Offline" Access

In order to post without having to logging (usefull if you aren't the only one posting) you need a permanent extended token. So you basically should have a separate script that you'll run once to retrieve that extended token and store it.

Getting an extended access token

To be able to post without the user being logged in you need a permanent access token for your page.

Here is a basic run down:

  • Get user to allow app with appropriate permissions (manage_pages, publish_stream)
  • Retreive user access token
  • Change user access token for extended user access token

Here is how I do this step (you could also use curl)

$token_url = "https://graph.facebook.com/oauth/access_token?client_id=YOUR_APP_ID&client_secret=YOUT_APP_SECRET&grant_type=fb_exchange_token&fb_exchange_token=OLD_TOKEN";
$accessToken = @file_get_contents($token_url);

Then

  • Query /me/accounts with user extended access token to get the page
  • Change page access token for extended access token (same code as above)

(The last step shouldn't be necessary according to the doc as you should get an extended page token when you query /me/accounts with an extended user token but in my case it didn't work)

And you get a permanent access token that only expires if the user changes password or disallows the app. All you have to do is store it with the page's id and retreive it wherever you need an API call to post to facebook.

The php sdk is pretty well documented so you shouldn't run into any problem a google search can't fix. Look for post september 2012 threads the flow hasn't changed since neither did the php sdk much.