通过 PHP 发布到 Facebook 粉丝页面的简单示例?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7818667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple example to post to a Facebook fan page via PHP?
提问by nmarti
I've done a lot of searching and I've found outdated tutorials that don't work...
我做了很多搜索,发现过时的教程不起作用......
I have a site made with PHP and when I submit a particular form in my admin area, I want to publish to my Facebook "fan page"
我有一个用 PHP 制作的网站,当我在管理区域提交特定表单时,我想发布到我的 Facebook“粉丝页面”
There is no RSS available, so do you have any example to directly post to the Facebook fan page (not user wall) using php sdk?
没有可用的RSS,所以你有什么例子可以使用php sdk直接发布到Facebook粉丝页面(不是用户墙)?
Thank you!
谢谢!
回答by nmarti
Finally, after a lot of tests, it worked, without the PHP SDK. This is the step by step guide:
最后,经过大量测试,它可以工作,没有 PHP SDK。这是分步指南:
1. Get permissions and the page token
1.获取权限和页面令牌
Go to https://developers.facebook.com/tools/explorer/and select your app from the first drop down menu, in the left.
转到https://developers.facebook.com/tools/explorer/并从左侧的第一个下拉菜单中选择您的应用程序。
Click on the button "Get access token", and in the "Select Permissions" window, click in "Extended Permissions" and check manage_pages and publish_stream, and click in "Get Access Token" blue button.
单击“获取访问令牌”按钮,在“选择权限”窗口中,单击“扩展权限”并选中 manage_pages 和 publish_stream,然后单击“获取访问令牌”蓝色按钮。
You may be asked in this step to grant permissions to your app to access to your Facebook account, accept.
在此步骤中,您可能会被要求向您的应用授予访问您 Facebook 帐户的权限,接受。
Next, click at the end of the text field next to the "GET" drop down, and replace the numbers for: me/accounts, and click in the blue button next to this text field.
接下来,单击“GET”下拉菜单旁边的文本字段末尾,替换以下数字:我/帐户,然后单击此文本字段旁边的蓝色按钮。
You'll get the tokens for all your pages, including your app page. Find your page name in the list, will look like this: "name": "Your page name"
您将获得所有页面的令牌,包括您的应用页面。在列表中找到您的页面名称,将如下所示:"name": "您的页面名称"
When you located your page, copy the access token for the page (will be really long), that can look like this: "access_token": "XXXXXXXX". Also copy the id of the page: "id": "XXXXX".
当你找到你的页面时,复制页面的访问令牌(会很长),看起来像这样:"access_token": "XXXXXXXX"。还要复制页面的 id:" id": "XXXXX"。
That's all for this step, we can start coding now.
这就是这一步的全部内容,我们现在可以开始编码了。
2. Post to your page wall via PHP
2.通过PHP发布到您的页面墙
First, for this script, you'll need a server supporting curl.
首先,对于这个脚本,你需要一个支持 curl 的服务器。
We start the PHP document defining the page access token and the page id that we've get in the 1st step:
我们开始定义页面访问令牌和我们在第一步中获得的页面 ID 的 PHP 文档:
<?php
$page_access_token = 'XXXXXXX';
$page_id = 'YYYYYYYY';
After that, we create an array with the info to post to our page wall:
之后,我们创建一个包含信息的数组以发布到我们的页面墙上:
$data['picture'] = "http://www.example.com/image.jpg";
$data['link'] = "http://www.example.com/";
$data['message'] = "Your message";
$data['caption'] = "Caption";
$data['description'] = "Description";
You can of course, use any other post parameter described in https://developers.facebook.com/docs/reference/api/post/and if you don't need one or many of the parameters above you can simply delete it.
当然,您可以使用https://developers.facebook.com/docs/reference/api/post/ 中描述的任何其他 post 参数,如果您不需要上述一个或多个参数,您可以简单地将其删除。
Ok, At this point we add to the array the access token:
好的,此时我们将访问令牌添加到数组中:
$data['access_token'] = $page_access_token;
And we set our post URL, to post in our page:
然后我们设置我们的帖子 URL,在我们的页面中发布:
$post_url = 'https://graph.facebook.com/'.$page_id.'/feed';
And the last step, we'll use a curl to post our message in our page wall:
最后一步,我们将使用 curl 在我们的页面墙上发布我们的消息:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $post_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$return = curl_exec($ch);
curl_close($ch);
?>
After that, we can save our PHP document, and try to execute it. The post may appear in our Facebook page.
之后,我们可以保存我们的 PHP 文档,并尝试执行它。该帖子可能会出现在我们的 Facebook 页面中。
Hope this code helps to other people with the same problem!
希望这段代码可以帮助其他有同样问题的人!
回答by bq23
You can test tokens using Facebook Access Token Debugger
您可以使用Facebook Access Token Debugger测试令牌
Working solution for API v.2.5
API v.2.5 的工作解决方案
- Get
code
forapp_id
as parameter ofresponse_uri
- 找
code
对app_id
作为参数response_uri
https://www.facebook.com/dialog/oauth?client_id=".$app_id."&redirect_uri=".$response_uri."&response_type=code&scope=manage_pages,publish_pages
https://www.facebook.com/dialog/oauth?client_id=".$app_id."&redirect_uri=".$response_uri."&response_type=code&scope=manage_pages,publish_pages
- Get
access_token
based oncode
,app_id
andapp_secret
as result ofresponse_uri
- 获取
access_token
基于code
,app_id
并app_secret
作为结果response_uri
https://graph.facebook.com/oauth/access_token?grant_type=authorization_code&client_id=".$app_id."&client_secret=".$app_secret."&code=".$code."&redirect_uri=".$response_uri
https://graph.facebook.com/oauth/access_token?grant_type=authorization_code&client_id=".$app_id."&client_secret=".$app_secret."&code=".$code."&redirect_uri=".$response_uri
- Get never expiring
page_access_token
forpage_id
based onaccess_token
- 获取永不过期
page_access_token
的page_id
基于access_token
https://graph.facebook.com/v2.5/".$page_id."?fields=access_token&access_token=".$access_token
https://graph.facebook.com/v2.5/".$page_id."?fields=access_token&access_token=".$access_token
回答by dikirill
As an addition to nmarti answer. Valid for API v.2.4.
作为 nmarti 答案的补充。适用于 API v.2.4。
If you don't want to go Facebook API console, rather do API calls, there are some instructions.
如果你不想去 Facebook API 控制台,而是进行 API 调用,这里有一些说明。
First of all, you have to have Facebook user, being admin on the page you want to post, also you have to create Facebook App in order to proceed.
首先,您必须拥有 Facebook 用户,是您要发布的页面的管理员,您还必须创建 Facebook 应用程序才能继续。
- Do login request, to get user token:
- 做登录请求,获取用户令牌:
In response, you should get %user-token%, save it, you will need in the next step.
作为回应,您应该获得 %user-token%,保存它,您将在下一步需要。
- Ask for long lived token:
- 请求长期存在的令牌:
Now you will have %long-lived-token%, required to get long lived page token.
现在您将拥有 %long-lived-token%,需要获得长期页面令牌。
- Now, get a list of your Facebook Pages,
- 现在,获取您的 Facebook 页面列表,
https://graph.facebook.com/v2.4/%page-admin-user-id%/accounts/?access_token=%long-lived-token%
https://graph.facebook.com/v2.4/%page-admin-user-id%/accounts/?access_token=%long-lived-token%
Find in the list your Page, and a page token, now you can continue with posting to page using nmarti example.
在列表中找到您的页面和页面令牌,现在您可以使用 nmarti 示例继续发布到页面。
Also Facebook says:
脸书也说:
The resulting page access token will not have any expiry time.
生成的页面访问令牌不会有任何到期时间。
回答by Lix
Here is the resource you are looking for. Scroll down to Page Login
and read from there.
You have to get an access token for your page and then use that token when posting. This is assuming that you want your post to appear "from the page". IE - posting as if you were the page.
这是您正在寻找的资源。向下滚动Page Login
并从那里阅读。您必须为您的页面获取访问令牌,然后在发布时使用该令牌。这是假设您希望您的帖子“从页面”出现。IE - 就像您是页面一样发布。
the actual call to the graph api to create a post object, and how to do it, can be found at this urlfrom the facebook documentation.
可以在 facebook 文档中的此 url 中找到对图形 API 的实际调用以创建 post 对象,以及如何执行此操作。