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.

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

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题