PHP Facebook API issue with business page - php

I have a Facebook app which posts news from a company website to the company's Facebook business page.
When I test it with my developer account's id or my private account id, it works great. But when I use the id of the business page, no news show up on the chronic.
This is my code:
<?php
require_once 'library/facebook.php';
$facebook = new Facebook(array(
'appId' => FACEBOOK_APP_ID,
'secret' => FACEBOOK_APP_SECRET,
'cookie' => true,
));
if(is_null($facebook->getUser())){
header("Location:{$facebook->getLoginUrl(array('req_perms' => 'user_status,publish_stream,user_photos,offline_access,publish_checkins'))}");
exit;
}
if($facebookpost){
try {
$facebook->setFileUploadSupport("http://" . $_SERVER['SERVER_NAME']);
$img = $sitepath.$gfxpath.$filename;
$arguments = array(
'source' => '#' . $img,
'message' => utf8_encode(html_entity_decode(strip_tags($text))),
);
$facebook->api("/".FACEBOOK_PROFILE_ID."/photos", 'post', $arguments);
} catch (FacebookApiException $e) {
print $e;
}
}
?>
The app shows up correctly in the profile setting (image)
are there some different settings for business pages?

You need to use the page's access token to post updates to your page. You can get the access token for the page by calling the following API endpoint:
/me/accounts, then look for your page ID / Name.

Related

How to Embed Posts from Website or Blog into Facebook Page using php

I am working on a module, where requirement is When Admin creates any new post on website or blog, then it should get post on its related Facebook Page also.
I want something like this. But its not working in my case.
Here is code
// require Facebook PHP SDK
// see: https://developers.facebook.com/docs/php/gettingstarted/
require_once("php-graph-sdk-5.5/src/Facebook/facebook.php");
require_once("php-graph-sdk-5.5/src/Facebook/autoload.php");
// initialize Facebook class using your own Facebook App credentials
// see: https://developers.facebook.com/docs/php/gettingstarted/#install
$config = array();
$config['app_id'] = 'myappud';
$config['app_secret'] = 'myappsecret';
$config['fileUpload'] = false; // optional
$fb = new \Facebook\Facebook($config);
// define your POST parameters (replace with your own values)
$params = array(
"access_token" => "myaccesstoek", // see: https://developers.facebook.com/docs/facebook-login/access-tokens/
"message" => "Here is a blog post about auto posting on Facebook using PHP #php #facebook",
"link" => "http://www.pontikis.net/blog/auto_post_on_facebook_with_php",
"picture" => "http://i.imgur.com/lHkOsiH.png",
"name" => "How to Auto Post on Facebook with PHP",
"caption" => "www.pontikis.net",
"description" => "Automatically post on Facebook with PHP using Facebook PHP SDK. How to create a Facebook app. Obtain and extend Facebook access tokens. Cron automation."
);
// post to Facebook
// see: https://developers.facebook.com/docs/reference/php/facebook-api/
try {
$ret = $fb->post('myfburl/feed', $params);
echo 'Successfully posted to Facebook';
}
catch(Exception $e) {
echo $e->getMessage();
}
It giving error as
An unknown error occurred
For more refer this link => http://www.pontikis.net/blog/auto_post_on_facebook_with_php
Help to figure it out where I m making mistake.
Updated :
<?php
// Include facebook class (make sure your path is correct)
use Facebook\Facebook;
require_once ("php-graph-sdk-5.5/src/Facebook/autoload.php");
require_once("php-graph-sdk-5.5/src/Facebook/Facebook.php");
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'app_id' => 'myappid',
'app_secret' => 'myappsecret',
'cookie' => true,
));
//$token is the access token from the URL above
$post = array('access_token' =>'myaccesstoken', 'message' => 'new test post - ' . date('Y-m-d'));
try{
$res = $facebook->post('myfburl/feed',$post);
print_r($res);
} catch (Exception $e){
echo $e->getMessage();
}
for posting to a page you would need a page token with the manage_pages and publish_pages permissions
Example :
https://www.facebook.com/dialog/oauth?client_id=yourClientId&redirect_uri=yourUri/&scope=manage_pages,publish_pages

Facebook App canvas Page not loading

I keep getting this error when I try to use my app I created for Facebook. When I go the the url for the app it take me to the login page, but when I log in I get a pop up box with the error.
App Not Setup: The developers of this app have not set up this app
properly for Facebook Login.
Every time I click ok it just reloads the popup again.
I have already changes the satatus to Live under status and review in my dashboard.
When I log into my account on facebook, it works fine, but I suppose this is because I am set as an admin for the app on the dashboard.
But other users get the problem above.
The code used:
require_once('facebook-php-sdk/src/facebook.php');
$facebook= new Facebook(array(
'appId' => $this->config->item('app_id'),
'secret' => $this->config->item('app_secret'),
'allowSignedRequest' => true,
));
$_REQUEST += $_GET;
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me?fields=picture.width(100).height(100),first_name,last_name,username,email');
} catch (FacebookApiException $e) {
$user = null;
}
} else {
$login_url = $facebook->getLoginUrl(array(
'scope' => 'email'
));
redirect($login_url, 'refresh');
}
This code is in my controller of my codeigniter application which I built the app.
How do I fix this?
UPDATE:
After I added the app as a canvas app this error seemed to go away, however there is still a problem.
I added the Facebook app platform and entered the canvas url, secure canvas url etc.
When I go to the app at the canvas page https://apps.facebook.com/MyApp/ - it work fine as I am a app administrator I guess.
However when an external user goes to the canvas page it just remain blank, but if a user goes to the canvas url http://apps.mydomain.com/MyAPP the app asks for their permissions and works fine as a website.
It just remains blank to users on the canvas page. Why doesn't the canvas page ask for permission?
Help Please.
EDIT:
require_once('facebook-php-sdk/src/facebook.php');
$facebook= new Facebook(array(
'appId' => $this->config->item('app_id'),
'secret' => $this->config->item('app_secret'),
'allowSignedRequest' => true,
));
$_REQUEST += $_GET;
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me?fields=picture.width(100).height(100),first_name,last_name,username,email');
} catch (FacebookApiException $e) {
$user = null;
}
} else {
$login_url = $facebook->getLoginUrl(array(
'scope' => 'email'
));
require_once('facebook-php-sdk/src/facebook.php');
$facebook= new Facebook(array(
'appId' => $this->config->item('app_id'),
'secret' => $this->config->item('app_secret'),
'allowSignedRequest' => true,
));
$_REQUEST += $_GET;
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me?fields=picture.width(100).height(100),first_name,last_name,username,email');
} catch (FacebookApiException $e) {
$user = null;
}
} else {
$login_url = $facebook->getLoginUrl(array(
'scope' => 'email'
));
echo("<script> top.location.href='" . $login_url . "'</script>");
}
Why doesn't the canvas page ask for permission?
Because you are redirecting inside of the iframe, but the FB login dialog does not want to be displayed in (i(frames) – for the obvious reason, that that would lead to a lot of phishing, since users are supposed to see that the page they are entering their login credentials too is actually the real FB site.
So you have to redirect within the top window instance, via JavaScript:
top.location.href = '…';

PHP Facebook SDK Post on Page as Admin [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Facebook Graph Api - Posting to Fan Page as an Admin
I am trying to have a form on my site where I can submit a blog post and then send that same blog post to my page on facebook.
My page is: https://www.facebook.com/foreveraloneminecraft
I want it to post it as if I was logged in and manually posted (like all the other posts on there).
I have been searching all over the place and everything I find either never actually posts something or throws an error.
This is what I got so far after searching all over:
<?php
include('core/init.inc.php');
require_once('libs/parser.php'); // path to Recruiting Parsers' file
$parser = new parser; // start up Recruiting Parsers
if(isset($_POST['submit'], $_POST['user'], $_POST['title'], $_POST['body'])
{
//--- Facebook Posting ------
require_once('libs/facebook/facebook.php');
$appid = '286964398044671';
$appsecret = 'myappsecret';
$pageid = '215852885143861';
$token = 'mytoken';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => $appid,
'secret' => $appsecret,
));
// Get the current access token
//$token = $facebook->getAccessToken();
//Information that makes up the facebook page post
$attachment = array(
'access_token' => $token,
'message' => $_POST['body'],
'link' => 'http://hgs1957.hostedd.com/news.php',
'picture' => 'http://hgs1957.hostedd.com/images/foreverAloneMCguy.png',
'name' => $_POST['title'],
'description'=> 'Latest news for foreveralonemc.com.'
);
//Try to post to the facebook page
try{
$res = $facebook->api('/'.$pageid.'/feed','POST',$attachment);
} catch (Exception $e){
echo $e->getMessage();
}
//Add the post to the sql database of posts
//add_post($_POST['user'], $_POST['title'], $_POST['body']);
//Redirect to news feed
header('Location: news.php');
die();
}
?>
I used the appsecret from the token is that I got when using https://developers.facebook.com/tools/explorer/ with the permissions: publish_stream, status_update, manage_pages, publish_actions, offline_access
After trying that it seems to post to the page wall but as me not as the page or admin.
Anyone know if this is just a bug or what I am doing wrong?
You should re-login to your app again to renew and store a new access token after it is expired.
I Hope This will help you.

How to publish message on facebook using php?

I have a facebook account created for my newsletter campagin
When I send the newsletter, i want to post the message to announce the mail is sent on that facebook post.
Currently i know how to invite people to adding me when they subscribe, but I do not know how to publish some text message on my page's board using php.
Is it possible and how to implemented?
Thank you
Using session instead of hardcoding the login name and password for a test page, but nothing appear in my facebook board?
<?php
require_once('plugin/facebook-sdk/src/facebook.php');
$facebook = new Facebook(array(
'appId' => 'APP_ID',
'secret' => 'APP_SECRET',
'cookie' => true,
));
$session = $facebook->getUser();
if($session) {
//post to wall/stream function
try {
$result = $facebook->api(
'/me/feed',
'post',
array(
'message' => 'testmessage'
)
);
} catch (FacebookApiException $e) {
echo 'Error: ' . print_r($e, true);
}
}
?>

Facebook login/connect help - PHP

I am having problems with Facebook connect.
Once I have the connect with Facebook button how do i retrieve the info with php?
There are loads of examples in developer.facebook.com but I couldn't get any of them to work.
Is it POST or GET or what? (I'm kinda new to php... )
Thanks.
DEMO: http://so.devilmaycode.it/facebook-login-connect-help-php/
Setup new App here
Download the PHP SDK here
Copy & Paste the code you'll find here into a file called index.php
Edit the first lines of the code above and provide the needed informations like below:
require 'sdk/facebook.php'; //the path to the downloaded PHP SDK
$facebook = new Facebook(array(
'appId' => '105810212821284', //App ID you find once created the app
'secret' => '3d6fdaa377cd4ca9...', //Secret Key you find once created the app
'cookie' => true,
));
you have done ;)
Application Settings:
Web Site -> Site URL -> http://so.devilmaycode.it/facebook/
Web Site -> Site Domain -> so.devilmaycode.it
Facebook Integration -> Canvas URL -> http://so.devilmaycode.it/facebook/
Requiriment
PHP5
CURL Lib
Check if you have the requirement
<? echo phpinfo(); ?>
Demo Source
http://pastebin.com/KyQ3CHV0
Use https://github.com/facebook/php-sdk/
Example
if ($session) {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
}
}
Display the data
echo $me['email']
http://developers.facebook.com/docs/reference/api/user/
More info about Graph API
To get the email or aditional data from users, you need to request it first(if not you only can get the basic data, like name / picture or only the public data), check the api facebook file in the function called "getLoginUrl".
'api_key' => $this->getAppId(),
'cancel_url' => $currentUrl,
'display' => 'page',
'req_perms' => 'email', <------------- email
'fbconnect' => 1,
'next' => $currentUrl,
'return_session' => 1,
'session_version' => 3,
'v' => '1.0',

Categories