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!
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.
In response, you should get %user-token%, save it, you will need in the next step.
Read More
Now you will have %long-lived-token%, required to get long lived page token.
Read More
Find in the list your Page, and a page token, now you can continue with posting to page using nmarti example.
Also Facebook says:
Read More
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.
You can test tokens using Facebook Access Token Debugger
Working solution for API v.2.5
code
forapp_id
as parameter ofresponse_uri
access_token
based oncode
,app_id
andapp_secret
as result ofresponse_uri
page_access_token
forpage_id
based onaccess_token
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:
After that, we create an array with the info to post to our page wall:
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:
And we set our post URL, to post in our page:
And the last step, we'll use a curl to post our message in our page wall:
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!