How to publish message on facebook using php? - 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);
}
}
?>

Related

How to fix "(#200) The user hasn't authorized the application to perform this action" error while posting on facebook wal

I am getting "(#200) The user hasn't authorized the application to perform this action" error when posting message on user's facebook wall. I am using facebook graph API. I have also set the "publish_actions" permission in the scope, but didn't working. My code is
require 'facebook/facebook.php';
$facebook = new Facebook(array(
'appId' => APP_ID,
'secret' => APP_SECRET,
));
$user = $facebook->getUser();
if ($user) {
$accessToken = $facebook->getAccessToken();
try {
//create message with token gained before
$post = array(
'access_token' => $accessToken,
'message' => 'Test message'
);
$res = $facebook->api('/me/feed', 'POST', $post);
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
} else {
# There's no active session, let's generate one
$login_url = $facebook->getLoginUrl(array(
'scope' => 'publish_actions'
));
header("Location: " . $login_url);
}
Can you please suggest me what i am missing in the code or in the app?
Since you mentioned that it does work for Admins, but not for anyone else, it´s very clear what the problem is: You need to go through Login Review with publish_actions - else, that permission will only work for users with a role in the App.
All the information you need about Login Review is in the docs: https://developers.facebook.com/docs/facebook-login/review

How to retrieve Facebook app user friends list with their details using php

ow i am developing social media web app i want to get app user friends list and their details i tried to retrieve user friends data using php sdk 4.0 but it gave me a empty array then i try to get data using json format. This is my code i am now trying
<?php
$fb_config = array(
'appId' => 'xxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxxx',
'cookie' => TRUE
);
$this->load->library('facebook', $fb_config);
$user = $this->facebook->getUser();
if ($user) {
try {
$data['user_profile'] = $this->facebook->api('/me');
} catch (FacebookApiException $e) {
$user = null;
}
}
if ($user) { //ok, the user exist, proceed
$access_token = $this->facebook->getAccessToken();
$file = file_get_contents('https://graph.facebook.com/me/friends?access_token=' . $access_token);
$jsonFriends = json_decode($file, true);
$Friends = $jsonFriends['data'];
}
?>
I am getting
Fatal error: Using $this when not in object context in M:\xampp\htdocs\fb\demo.php on line 7
This error when i run this code.
you need to change
$this->load->library('facebook', $fb_config);
to
FacebookSession::setDefaultApplication('YOUR_APP_ID', 'YOUR_APP_SECRET');
as a start.
This array is usually for JavaScript SDK
$fb_config = array(
'appId' => 'xxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxxx',
'cookie' => TRUE
);
And from where did you get your access token or the session.
Did you use another page with JS sdk? because I can't find an instance of "FacebookRedirectLoginHelper" or "FacebookJavaScriptLoginHelper".
I really recommend that you start by going through the documentation by facebook in here:
https://developers.facebook.com/docs/php/gettingstarted/4.0.0

Posting a comment on the news feed

Hey all i am using the following code to post to a posting on my news feed:
<?php
require '../src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'xxxxxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'fileUpload' => true,
'cookie' => true
));
$user = $facebook->getUser();
if ($user) {
try {
$access_token = $facebook->getAccessToken();
$user_profile = $facebook->api('/me');
$comment = $facebook ->api('/xxxxxxxxxxxxxx/comments',
'POST',
array(
'access_token' => $access_token,
'message' => 'testing!'
)
);
} catch (FacebookApiException $e) {
echo ($e);
$user = null;
}
}
<?php if ($user): ?>
Logout
<?php else: ?>
Login with Facebook
<?php endif ?>
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$statusUrl = $facebook->getLoginStatusUrl();
$params = array(
'scope' => 'read_stream, friends_likes, email, read_mailbox, read_requests, user_online_presence, friends_online_presence, manage_notifications, publish_actions, publish_stream, user_likes, user_photos, user_status, user_videos, read_insights'
);
$loginUrl = $facebook->getLoginUrl($params);
}
?>
<?php print_r($user_profile); ?>
For some reason i get this error:
OAuthException: (#221) Photo not visible
And i have no idea since i am posting a text comment and not even an image??
If i comment out the code line $comment = $facebook ->api('/xxxxxxxxxxxxxx/comments',
'POST',
array(
'access_token' => $access_token,
'message' => 'testing!'
)
); it works just fine (as in, displays my info with user_profile). I've tried reading over the page that tells you how to use the comments here and i do - it just doesn't seem to want to work?
What am i missing???
update
using the graph API i was able to do the same thing i am trying to do via PHP so i know it works...:
I know it's an old question but...
First, for tests the same enviroment, you have to use your app token, not the Graph API Explorer. Otherwise, you're testing the API with all the permissions.
Second, your problem must be a permission thing (that's why it's working on the API Explorer). You should:
Ask for publish_permission on the login (not recommended) or,
Ask for publish_permission when the user is ready to send the comment.
More information about this: Optimizing Permissions Requests
Here the permissions that you need: Publishing Comments
Permissions
As a general rule, when you see a OAuthException, you are having a permission issue.
If the post is photo type, you're going to need a user_photos permission.

post message on facebook wall not working

I want to post a message on facebook wall from our site. First I am getting error as
Uncaught OAuthException: (#200), when trying to post on wall
Now I am not getting the error but the code is not working.
$facebook = new Facebook(array(
'appId' => APP_ID,
'secret' => APP_SECRET,
'cookie' => true,
'req_perms' => 'email,read_stream,read_friendlists,publish_stream,offline_access,manage_pages',
));
$user = $facebook->getUser();
$token = $facebook->getAccessToken();
if ($user) {
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if (!empty($user_profile )) {
$username = $user_profile['name'];
$uid = $user_profile['id'];
try {
$post=$facebook->api("/".$uid."/feed", "post", array(
'access_token' => $token,
'message' => 'test',
));
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
But no message is coming what to do? Is there any problem while creating the application?
Here's my wild guess, since our Comment Q&A wasn't going to work very well.
If you authorize your app with some set of permissions, and subsequently change the permissions that your app requests (at the app admin page), your existing authorization does not update to include the new permissions. Your app will still function, but the actions that require the new permissions will fail.
You can test if this is causing your error by simply removing the app from your personal account app settings page, and re-approving the app with the new permissions. If your problem vanishes, then congratulations. If not, you'll need to do more detective work. Your code appears to be fine.

PHP Facebook API issue with business page

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.

Categories