通过 PHP 发布到 Facebook 粉丝页面的简单示例?

发布于 2024-12-10 17:18:07 字数 174 浏览 0 评论 0 原文

我做了很多搜索,发现过时的教程不起作用...

我有一个用 PHP 制作的网站,当我在我的管理区域提交特定表单时,我想发布到我的 Facebook“粉丝页面”

没有可用的 RSS,所以您有任何使用 php sdk 直接发布到 Facebook 粉丝页面(而不是用户墙)的示例吗?

谢谢你!

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"

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?

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

标点 2024-12-17 17:18:07

最后,经过大量测试,在没有PHP SDK的情况下,它可以工作。这是分步指南:

1.获取权限和页面令牌

转到https://developers.facebook.com/tools/explorer/ 并从左侧第一个下拉菜单中选择您的应用程序。

单击“获取访问令牌”按钮,在“选择权限”窗口中,单击“扩展权限”并选中manage_pages和publish_stream,然后单击“获取访问令牌”蓝色按钮。

在此步骤中,系统可能会要求您授予应用访问您 Facebook 帐户的权限,请接受。

接下来,单击“GET”下拉列表旁边的文本字段末尾,并将数字替换为:me/accounts,然后单击该文本字段旁边的蓝色按钮。

您将获得所有页面(包括应用程序页面)的令牌。在列表中找到您的页面名称,将如下所示:“名称”:“您的页面名称”

当您找到页面时,复制该页面的访问令牌(会很长),即看起来像这样:"access_token": "XXXXXXXX"。另请复制页面的 ID:“id”:“XXXXX”

这就是这一步的全部内容,我们现在可以开始编码了。

2.通过 PHP 发布到您的页面墙

首先,对于此脚本,您需要一个支持curl 的服务器。

我们启动 PHP 文档,定义页面访问令牌和我们在第一步中获得的页面 id:

<?php
$page_access_token = 'XXXXXXX';
$page_id = 'YYYYYYYY';

之后,我们创建一个包含要发布到页面墙的信息的数组:

$data['picture'] = "http://www.example.com/image.jpg";
$data['link'] = "http://www.example.com/";
$data['message'] = "Your message";
$data['caption'] = "Caption";
$data['description'] = "Description";

当然,您可以使用任何其他帖子https://developers.facebook.com/docs/reference/api/post/ 中描述的参数如果您不需要上述一个或多个参数,只需删除即可 它。

好的,此时我们将访问令牌添加到数组中:

$data['access_token'] = $page_access_token;

我们设置发布 URL,以在页面中发布:

$post_url = 'https://graph.facebook.com/'.$page_id.'/feed';

最后一步,我们将使用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);
?>

之后,我们可以保存我们的 PHP 文档,并尝试执行它。该帖子可能会出现在我们的 Facebook 页面上。

希望这段代码可以帮助其他遇到同样问题的人!

Finally, after a lot of tests, it worked, without the PHP SDK. This is the step by step guide:

1. Get permissions and the page token

Go to https://developers.facebook.com/tools/explorer/ and select your app from the first drop down menu, in the left.

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.

You may be asked in this step to grant permissions to your app to access to your Facebook account, accept.

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.

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"

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".

That's all for this step, we can start coding now.

2. Post to your page wall via PHP

First, for this script, you'll need a server supporting curl.

We start the PHP document defining the page access token and the page id that we've get in the 1st step:

<?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.

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:

$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:

$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.

Hope this code helps to other people with the same problem!

拥抱我好吗 2024-12-17 17:18:07

您可以使用测试令牌
Facebook 访问令牌调试器

API v.2.5 的工作解决方案

  1. 获取app_idcode 作为 response_uri 的参数

https://www.facebook.com/dialog/oauth?client_id=".$app_id."&redirect_uri=".$response_uri."&response_type=code&scope=manage_pages,publish_pages< /代码>


  1. 根据 codeapp_idapp_secret 获取 access_token 作为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

  1. 根据page_id获取永不过期的page_access_token access_token

https://graph.facebook.com/v2.5/".$page_id."?fields=access_token&access_token=".$access_token

You can test tokens using
Facebook Access Token Debugger

Working solution for API v.2.5

  1. Get code for app_id as parameter of response_uri

https://www.facebook.com/dialog/oauth?client_id=".$app_id."&redirect_uri=".$response_uri."&response_type=code&scope=manage_pages,publish_pages

  1. Get access_token based on code, app_id and app_secret as result of 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

  1. Get never expiring page_access_token for page_id based on access_token

https://graph.facebook.com/v2.5/".$page_id."?fields=access_token&access_token=".$access_token

流云如水 2024-12-17 17:18:07

作为 nmarti 答案的补充。适用于 API v.2.4。

如果您不想进入 Facebook API 控制台,而是想进行 API 调用,这里有一些说明。

首先,您必须拥有 Facebook 用户,是您要发布的页面的管理员,并且您还必须创建 Facebook 应用程序才能继续。

  1. 执行登录请求,获取用户令牌:

https://www.facebook.com/dialog/oauth?client_id=%app-id%&redirect_uri=%your-site-url%&response_type=token&scope=manage_pages,publish_pages< /a>


作为响应,您应该获得%user-token%,保存它,您将在下一步中需要。

了解更多

  1. 请求长期令牌:

https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id=%app-id%&client_secret=%app-secret%&fb_exchange_token=%user-token%< /a>


现在您将拥有 %long-lived-token%,这是获取长期页面令牌所必需的。

了解更多

  1. 现在,获取您的 Facebook 页面列表,

https://graph.facebook.com/v2.4/%page-admin-user-id%/accounts/?access_token=%long-lived-token%

在列表中找到您的主页和页面令牌,现在你可以继续使用 nmarti 示例发布到页面。

Facebook 还表示:

生成的页面访问令牌不会有任何到期时间。

了解更多

As an addition to nmarti answer. Valid for API v.2.4.

If you don't want to go Facebook API console, rather do API calls, there are some instructions.

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.

  1. Do login request, to get user token:

https://www.facebook.com/dialog/oauth?client_id=%app-id%&redirect_uri=%your-site-url%&response_type=token&scope=manage_pages,publish_pages

In response, you should get %user-token%, save it, you will need in the next step.

Read More

  1. Ask for long lived token:

https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id=%app-id%&client_secret=%app-secret%&fb_exchange_token=%user-token%

Now you will have %long-lived-token%, required to get long lived page token.

Read More

  1. Now, get a list of your Facebook Pages,

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.

Also Facebook says:

The resulting page access token will not have any expiry time.

Read More

快乐很简单 2024-12-17 17:18:07

这是您正在寻找的资源。向下滚动到页面登录并从那里阅读。
您必须获取页面的访问令牌,然后在发布时使用该令牌。这是假设您希望您的帖子“从页面”显示。 IE - 发帖就像你是页面一样。

实际调用图形 api 来创建 post 对象以及如何执行此操作,可以在 此 url 来自 Facebook 文档。

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.

the actual call to the graph api to create a post object, and how to do it, can be found at this url from the facebook documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文