通过 PHP 发布到 Facebook 粉丝页面的简单示例? [英] Simple example to post to a Facebook fan page via PHP?

查看:29
本文介绍了通过 PHP 发布到 Facebook 粉丝页面的简单示例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我进行了大量搜索,发现过时的教程不起作用...

I've done a lot of searching and I've found outdated tutorials that don't work...

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

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"

没有可用的RSS,所以你有什么例子可以使用php sdk直接发布到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?

谢谢!

推荐答案

终于,经过大量测试,它可以工作,没有 PHP SDK.这是分步指南:

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

1.获取权限和页面令牌

转到 https://developers.facebook.com/tools/explorer/ 并从左侧的第一个下拉菜单.

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

点击Get access token"按钮,在Select Permissions"窗口中,点击Extended Permissions",勾选manage_pages和publish_stream,点击Get Access Token"蓝色按钮.

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.

在此步骤中,您可能会被要求向您的应用授予访问您 Facebook 帐户的权限,接受.

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

接下来,单击GET"下拉菜单旁边的文本字段末尾,并替换以下数字:me/accounts,然后单击此文本字段旁边的蓝色按钮.

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.

您将获得所有页面的令牌,包括您的应用页面.在列表中找到您的页面名称,将如下所示:"name": "您的页面名称"

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"

找到页面后,复制该页面的访问令牌(会很长),如下所示:"access_token": "XXXXXXXX".同时复制页面的id:"id": "XXXXX".

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.通过 PHP 发布到您的页面墙

首先,对于这个脚本,你需要一个支持 curl 的服务器.

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

我们开始定义页面访问令牌和我们在第一步中获得的页面 ID 的 PHP 文档:

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

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

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;

然后我们设置我们的帖子 URL,在我们的页面上发布:

And we set our post URL, to post in our page:

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

最后一步,我们将使用 curl 在我们的页面墙上发布我们的消息:

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);
?>

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

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!

这篇关于通过 PHP 发布到 Facebook 粉丝页面的简单示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆