dtfbj24048 2013-11-11 13:37
浏览 84
已采纳

使用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?

  • 写回答

1条回答 默认 最新

  • dslh32311 2013-11-11 14:41
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口