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

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

问题描述

我已经做了大量的搜索,我发现过时的教程不起作用。



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



没有可用的RSS,所以你有直接的例子发贴到Facebook粉丝专页(非用户墙)使用php sdk?



谢谢!

解决方案

最后,经过很多测试,它没有PHP SDK。这是一步一步的指导:



1。获取权限和页面令牌



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



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



您可能会在此步骤中询问您的应用访问您的Facebook帐户的权限,接受。



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



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



找到您的页面时,复制页面的访问令牌(将很长),可以看起来像这样:access_token:XXXXXXXX。还要复制页面的ID: id:XXXXX



这一切都是为了这一步,我们现在可以开始编码。



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



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



我们启动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'] =你的消息;
$ data ['caption'] =Caption;
$ data ['description'] =描述;

您当然可以使用 https://developers.facebook.com/docs/reference/api/post/ ,如果您不需要一个或多个上面的参数可以简单地删除它。



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

  $ data ['access_token'] = $ page_access_token; 

我们设置我们的发布网址,以发布在我们的页面中:

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

最后一步,我们将使用卷曲在我们的页面墙上发布我们的消息: / p>

  $ 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页面。



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


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!

解决方案

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!

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

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