How do I post on Facebook User's wall? - php

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

Related

Facebook Graph search API

Background:
I'm an employee of a public figure who is constantly getting facebook scam impersonations. I'm going to automate the process of checking facebook for new scams by having a script search facebook each day and report any new users and pages for his name. I'm modifying an old FB app that I created years ago to run the process.
Problem:
I noticed that I can search for "page" on facebook's graph api with my app, but "user" comes back with: "Fatal error: Uncaught OAuthException: A user access token is required to request this resource." I assume this is a permissions error so I try to add an &Access_Token= with all permissions from (https://developers.facebook.com/tools/explorer) to the URL with no luck.
Here's my PHP script:
require '../src/facebook.php';<BR>
$facebook = new Facebook(array(
'appId' => '**I_removed_this_code**',
'secret' => '**I_removed_this_code**'));
$access_token = $facebook->getAccessToken();
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');<BR>
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
$fb_response = $facebook->api('/search?q=**Public_Figure_Name**&type=page'); //<--change this to user and it doesnt work
print_r(array_values($fb_response));
The basic issue is that only users an app has access to are either users of the app or (given proper friend permissions) friends of users of the app.
However, if you were to act as you, using an SSO token, 'you' would have a user token and can do graph searches.
PS. I would have added this as a comment but my rep's not high enough.
If I try the following in the Graph Explorer, it works without problem:
/search?q=Tom%20Cruise&type=user
https://developers.facebook.com/tools/explorer?method=GET&path=search%3Fq%3DTom%2520Cruise%26type%3Duser&version=v2.2
The Search API is documented at https://developers.facebook.com/docs/graph-api/using-graph-api/v2.2#search which describes exactly your behaviour (distinction between page and user search).

Posting testimonials to Facebook business page from website cms

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);

Can't access mail value in facebook sdk although permission

Hi I have a problem with the Facebook API although I am asking for the permission to get the email I can't get the mail value, what do I do wrong? Here my code:
$facebook = new Facebook(array(
'appId' => 'xxxxxxxx',
'secret' => 'xxxxxxxxxx',
'cookie' => false
));
// See if there is a user from a cookie
$fbuser = $facebook->getUser();
if ($fbuser) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$FBUSER = $facebook->api('/me');
$FBUSER1 = $facebook->api('/me');
print_r($FBUSER1);
} catch (FacebookApiException $e) {
print_r($e);
$USER=false;
}
}
$loginUrl = $facebook->getLoginUrl(array("scope"=>"email, publish_actions"));
A few days ago I was coding a similar login/authorization bit of my app and I had similar results until I properly ordered the flow steps.
From your snippet it looks like you print the $fbuser array before the user actually grants the extended permissions to your app. Let's imagine you were doing some tests with your own account being logged in to facebook: you might have authorized the app once with the basic permission and now it returns these basic data from your account (with no "email" as it belongs to the extended perms).
More code could help to better understand but I would still double check that:
1. the page/popup coming from the FB getLoginURL() method is shown and the user accepts both to login (if not) and to authorize the extended permissions if needed.
2. the return_uri code on your website is ready to catch any data coming from the previous step (eg. print the user details).
Watchout as I see from your code that the return_uri is not defined.
If you send request without access_token, you will get short field set without email and some different fields.

Facebook PHP SDK/Graph API - Returning MY name/info, but not anyone elses?

I'm trying to use the Graph API and the PHP SDK for Facebook to try and return the current users name, but I'm only having luck with it displaying my name. This is pretty straight forward and someone that knows the Graph API shouldn't have any problem helping me out just by looking at the code I'm using.
require 'facebook.php';
$appid=get_post_meta($thisID,'_EFBPDAppID',true);
$appsec=get_post_meta($thisID,'_EFBPDAppSec',true);
$facebook = new Facebook(array(
'appId' => $appid,
'secret' => $appsec,
'cookie' => true
));
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
And then I use this: echo $user_profile['name']; to display the users name, but it is only displaying my name. On any other profile it just shows where that variable is as blank. On my profile, it shows my name. I also used print_r($user); and it is displaying my ID, but for any other profile it displays 0.
Do users need to authenticate themselves or should there already be a session with this info? How do I get that session info if there is one?
Anyone who can help out I would be very grateful. Thanks.
For security and privacy as well, Facebook users must login and allow the current application to have access to their account information. You can accomplish this using different SDK. Most people use Javascript but I persoally prefer PHP. As simple implementation in HTML is shown below:
Login to Facebook
It will generate something like:
Login to Facebook
you should be able to access the user's information after that process using Graph API.
$fbuser = $facebook->api('/me');
echo "Hello ".$fbuser['name'];
Let me know if works for you.
http://www.9lessons.info/2011/02/login-with-facebook-and-twitter.html
Your user must authenticate the application that your going to create.
After that facebook will redirect with the access_token (and the process is called as callback.)
Save the access_token and user id so that you can access data or Post message automatically without login into the facebook.
I've got the rectification for your problem. I use a simple Facebook application. It works but under development.
You must authorize the user first and get his user ID. After that just replace the "me" by user_id, and you are done.
<?php
require_once 'facebook-php-sdk/src/facebook.php';
$facebook = new Facebook(array(
'appId' => '[your app id]',
'secret' => '[your app secreat]',
'cookie' => true,
));
// Then authorize the current user in the way you desire
// get his user_id
$user_profile = $facebook->api('/[the user's user id]');
echo $user_profile['name'];
?>

Can I store Facebook access token and use it later?

I am building a web app (PHP) that uses FB connect. I successfully register / sign in user with the help of the PHP lib provided by facebook. Also I can post to wall, using this code
Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false;
Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYHOST] = 2;
$facebook = new Facebook(array(
'appId' => $fb_key,
'secret' => $fb_secret,
'cookie' => true, // enable optional cookie support
));
$session = $facebook->getSession();
if ($session)
{
$facebook->api('/me/feed', 'POST', array('message'=>$message, 'link'=>$link['href'], 'name'=>$link['text']));
}
However, if I manually go to my browser's cookie manager and delete the cookie that stores FB session, the code doesn't work. The only thing I have is user's FB ID which I store in DB. Is there any way to post to user's wall even if FB sessions is lost? Does it make sense to store user's FB access token in DB to post to wall later or is the access token relatively short-lived?
Here's an example situation that might happen in my app:
user clicks fb button, authorizes my app, gets redirected back to my site where I automatically create an account based on data provided by FB, also I store user's FB ID so that I could sign in this user later. Now he browses site, enters some info and this info gets posted to his wall. Everything is fine so far because user's browser holds the cookie created by FB. Now user leaves the site and contacts site admin. Admin opens his own browser, goes to admin interface and posts something on behalf of this user. Now, having that user's FB ID and assuming that user hasn't revoked permissions, can I still post this to his wall?
With the Facebook PHP SDK v3 (see on github), it is pretty simple to ask and use a user offline access token. Here is how you do that.
Get the offline access token
First you check if the user is logged in or not :
require "facebook.php";
$facebook = new Facebook(array(
'appId' => YOUR_APP_ID,
'secret' => YOUR_APP_SECRET,
));
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
$user = null;
}
}
If he is not, you generate the "Login with Facebook" URL asking for the offline_access permission :
if (!$user) {
$args['scope'] = 'offline_access';
$loginUrl = $facebook->getLoginUrl($args);
}
And then display the link in your template :
<?php if (!$user): ?>
Login with Facebook
<?php endif ?>
Then, when the user is logged in, you can retrieve the offline access token and store it. To get it, call :
if ($user) {
$token = $facebook->getAccessToken();
// store token
}
Use the offline access token
To use the offline access token when the user is not logged in :
require "facebook.php";
$facebook = new Facebook(array(
'appId' => YOUR_APP_ID,
'secret' => YOUR_APP_SECRET,
));
$facebook->setAccessToken("...");
And now you can make API calls for this user :
$user_profile = $facebook->api('/me');
Hope that helps !
UPDATE: This answer is no longer valid as offline_access is deprecated.
You need to request the offline_access permission. Check the permissions doc.
EDIT Per the update and comments - some info on the removal of the offline_access can be found here.

Categories