Posting testimonials to Facebook business page from website cms - php

Sorry if this has been dealt with before, but my website guy is having issues getting this to work.
I am using facebook open graph api to post a testimonial from my PHP website CMS to my facebook business page.
I have a facebook profile and created a business page from my profile.
I have also created an app under my profile to get an app id and secret key which is used for the graph api to post on behalf of me from my website to my facebook businness page. The issue is I need the post to go directly to my business page and be posting as my business, not as or on behalf of me. Currently the api is sending the posts directly to my profile wall showing as me sharing a link via my business page and it doesn't appear on the business page at all. I need a way to post the testimonial to my business page directly rather than on my profile wall. How do I do that? My understanding is he has used the personal ID & App secret and also the business page app ID & secret, but seem to get the same result.
The code below is what is being used, any assistance would be greatly appreciated;
<?php
$facebook = new Facebook(array(
'appId' => 'myappid',
'secret' => 'mysecrectkey',
));
// Get User ID
$user = $facebook->getUser();
if ($user) {
try
{
$user_profile = $facebook->api('/me');
$AccessToken = $facebook->getAccessToken();
$data = array("message" => $pdes,"link" => "http://www.website.com.au/testimonial.php", "name" => $name);
$share = $facebook->api("/me/feed", "POST", $data);
}
catch (FacebookApiException $e)
{
error_log($e);
$user = null;
}
}
?>

Well, your problem is here:
$share = $facebook->api("/me/feed", "POST", $data);
This code sends message to your wall, instead of the page. In order to post on your Facebook page on behalf of your page, first, your app needs to get manage_pages and publish_stream permissions. Once you have it you need to do is get the access token of the page you want to post on:
$pages = $facebook->api('/me/accounts');
foreach($pages as $page)
{
if($page['id'] == YOUR_PAGE_ID)
$data['access_token'] = $page['access_token'];
}
Once you have page access token you must use it to send message on your page's wall:
$facebook->api(YOUR_PAGE_ID . '/feed', 'POST', $data);

Related

How do I post on Facebook User's wall?

I have some Facebook user in my database with their facebook id which is I have got after they login via my facebook app. Now I want to post their wall after a certain period using php.
I have got their user id using below code-
include_once "facebook.php";
$facebook = new Facebook(array(
'appId' => xxxxxxxxxxxxxxx,
'secret' => xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
'cookie' => true,
));
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
print_r($e);
$user = null;
}
}
I have got their id from $user_profile variable.
I am using this scope to login "email,publish_actions"
Please give me some sample code if possible, I'm too much worry about this :(
Sorry for my poor English
As of 2nd October 2013 you can no longer post to another user's wall using the API:
Removing the ability to post to friends' timelines via API
We have found that posting content via API (stream.publish) on a
friend's wall lead to a high incidence of user dissatisfaction (hiding
content, blocking the app). After the migration period, posting
content to friends' timelines via stream.publish will no longer be
allowed. Please use the Feed Dialog for posting.
Source: https://developers.facebook.com/docs/apps/migrations/completed-changes

Create a Facebook App that can post to a page I am an admin of

As part of the website I am working on, I need to be able to post directly to the wall of my clients Facebook Page.
I have created an App and am successfully posting to my own dummy profile by simply using:
$request = new FacebookRequest(
$session, 'POST', '/me/feed', array(
'link' => 'my_url',
'message' => 'my_message'
);
Now I need to modify this to post to my clients page, im assuming I cant just change 'me' to by their page ID because that would be a massive security flaw so I need to get permission somehow to post to their page. I am an admin for the page, how can I do this? Or do I actually need to be logged in with the user account that created the page?
I have looked all around in the developer section at creating an app but there doesn't seem to be any way of creating an app for a different account.
Any advice would be greatly appreciated on this.
Many thanks
In order to post to a Facebook page, you need to go through a two steps process -
login as the user using the extended user token,
get the user's token for the page
post to the page using the page's token.
The following code should be able to post a message to your page. I HAVEN'T DEBUGGED IT since it is simplified from my own web site :-) Don't forget to include the Facebook SDK files:
function extract_access_token_of_facebook_page_by_id($array_of_all_user_pages,$page_id){
$num_of_pages=count($array_of_all_user_pages['data']);
$the_page_token="";
for ($i=0; $i<$num_of_pages; $i++){
if ($array_of_all_user_pages['data'][$i]['id']==$page_id){
$the_page_token=$array_of_all_user_pages['data'][$i]['access_token'];
}
}
return $the_page_token;
}
$facebook = new Facebook(array(
'appId' => $YOUR-APP-ID,
'secret' => $YOUR-APP-SECRET,
));
try
{
$facebook->setAccessToken($THE-USER-TOKEN);
$page_id = YOUR-PAGE-ID;
$fanpage_access_token=extract_access_token_of_facebook_page_by_id($facebook->api("/me/accounts"),$page_id);
$args = array();
$args['access_token'] = $fanpage_access_token;
$args['message'] = "THE TEXT YOU WANT TO POST";
$api_url="/".$page_id."/feed";
$api_response = $facebook->api($api_url, 'post', $args);
}
catch (FacebookApiException $e)
{
echo 'Error facebookservice'.$e;
}
Please look into Application developer section. It will help you. Create on developer account and you will be able to access page.
you code is for old sdk version i think, it not work for me : Fatal error: Class 'Facebook' not found

Facebook Php Api basics

Im getting started with the php sdk, and struggling to understand a few things (I have a basic example below - but everytime the pic goes to MY wall and not the fan page)
Code:
require_once("facebook.php"); // set the right path
$config = array();
$config['appId'] = 'app id';
$config['secret'] = 'app secret';
$config['fileUpload'] = true; // optional
$fb = new Facebook($config);
$params = array(
// this is the access token for Fan Page
"access_token" => "I create this via the graph api (select App and click get token), I select publish stream and photo_upload permissions.",
"message" => "test message",
"source" => "#" ."photo.png", // "#" .
);
try {
// **** is Facebook id of Fan page
$ret = $fb->api('/****/photos', 'POST', $params);
echo 'Photo successfully uploaded to Facebook Album';
} catch(Exception $e) {
echo $e->getMessage();
}
the picture keeps going to MY wall (logged in user) and not the fan page, is that because im getting a token for the currently logged in user (me) instead of the fan page? and if so how do i generate a token for the fan page whilst logged in as the developer? (is it by putting the fan page id into the Get bar?)
Apologies for the noob questions but the facebook documentation sucks (until u understand the core functionality of course). Most tutorials work once u know how to use the graph API - none ive looked actually EXPLAIN how to use the graph to generate correct tokens, etc.
\POST /<ID>/photos — this will post on the <ID>'s wall only, so please double-check the <ID> that you are using.
The access token will depict that on behalf of whom the photo would be posted on the <ID>'s wall. If you use the user access token, the photo will be published on behalf of the user and if page access token is used, it will be published on the page's behalf itself.
You can get the page access token of your pages by-
\GET /me/accounts?fields=access_token
(permission required: manage_pages)
Demo

Codeigniter and facebook php sdk autopost to company wall

I have built a site with codeigniter and implemented facebook login for users. Everything is working great there. App is connected user tokens beig saved the whole nine yards. What I am having trouble doing is having the website itself NOT the logged in user post to the connected company facebook page on a user post completion. essentially the user posts a listing. the website then on submit of that posting posts to my facebook company page NOT the logged in users facebook wall. (i have that working already. Can i leave the website logged into Facebook to post to the wall while a user is logged on as well?
You will need to get the Appid, App secret and an access token. You can extend the access token so that it doesn't expire.
$graphUrl = 'https://graph.facebook.com/oauth/access_token?client_id='.APPID .'&client_secret='.APPSECRET.'&grant_type=fb_exchange_token&fb_exchange_token='.ACCESS_TOKEN;
$accessToken = #file_get_contents($graphUrl);
parse_str($accessToken); //get the access_token param in the string and would be named $access_token
if(!$access_token) $access_token = $accessToken; //if cannot be extended then just return the access token with 2 hours expiry
In order to post as your company page, you will need their page id.
Here is part of my script that I used to get all this working. Its not codeigniter, but you will be able to see how it works.
$config = array(
'appId' => APPID,
'secret' => APPSECRET,
);
$facebook = new Facebook($config);
$facebook->setAccessToken(ACCESS_TOKEN);
// Get User ID
$user = $facebook->getUser();
if ($user) {
try {
$page_id = '************'; //
$page_info = $facebook->api("/$page_id?fields=access_token");
if(!empty($page_info['access_token']) ) {
// do your code stuff
}
} catch etc etc
Hope this is helpful to you

How to Publish on Facebook Page automatic from php server scripts?

My client has a e-commerce website where they sell their products and they also have facebook page for that website. I want to to publish wall post on that page when ever the product price is reduced or new product is lunched. But i want to make this automatic, so i want to authorize facebook automatic, means that i don't want any login dialog box. the script should authorize itself. now the code i am using is as below but it ask me for log in. and also tell me which method is good(easy) to publish page wall post?
require 'API_Library/Facebook/src/facebook.php';
$page_id = '111111111111111';
$appId = '111111111111111';
$appSecret = 'aaaaaaaaaaaaaaaaaaaaa22222222222';
$facebook = new Facebook(array(
'appId' => $appId,
'secret' => $appSecret,
'cookie' => true
));
$session = $facebook->getAccessToken();
$me = NULL;
if($session)
{
try
{
$me = $facebook->api('/me');
$facebook->api('me/feed', 'post', array(
'message' => 'Hello World!'
));
}
catch(FacebookApiException $e)
{
echo $e->getMessage();
}
}
OUTPUT:
(#200) The user hasn't authorized the application to perform this action
In order to post something to a page, you will need the administrator of the page to be authenticated with the application. You cannot post to a page without being authenticated. So there will be some user interaction required before you can set this up automatically.
The process would be as follows:
Setup a Facebook App to get a App ID and app secret - you'll need this to query the API and post updates to the page
As the page administrator, login to your app with the manage_pages permission.
After logging in, an API call to /me/accounts will give you an access_token to access the page and publish updates
Using the page's accesss_token, you can then POST updates using /{page_id}/feed
Hope this helps.

Categories