I have a website and a facebook page for the website. For most of the news update, I post them on the facebook page's wall.
Now I want to show the wall's content of that facebook page on my website, just like the facebook's Like Box with "stream" enabled: http://developers.facebook.com/docs/reference/plugins/like-box/
However, I only want to show the stream, and perfectly can show it in my own presentation. So is it possible to get only the content of a facebook page by facebook's API?
I think if you use the SDK you can get it by using
fb->api("/{id}/feed");
There's a php and a javascript sdk.
But you also need an access token now to get the feed of a page.
EDIT: Here's a copy of example.php (from the php-sdk) modified for your purpose.
include_once("facebook-sdk/facebook.php");
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'APP_ID GOES HERE',
'secret' => 'SECRET GOES HERE',
));
// Get User ID
$user = $facebook->getUser();
// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
// This is where we grab the posts
$wall_posts = $facebook->api('/courseyou/posts');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl();
}
?>
<?php if ($user): ?>
Logout
<?php else: ?>
<div>Login with Facebook</div>
<?php endif ?>
<h3>Wall Posts</h3>
<?php
foreach ($wall_posts["data"] as $post) {
echo "<p>".$post["from"]["name"].": ".$post["message"]."</p>";
}
?>
As far as I know you need an access token to view a page's posts/feed with the api, which makes me think that you need the user to login with facebook.... I'm fairly new to this, but you should look into how to get an access token, because you need one for this.
Related
Hello I would like to check if user is already fan of my fanpage. I already found ready script to do that but it have so much parsing errors. Could you help me and fix that code?
<!--?php require('facebook.php');
$config = array(
'appId' =?--> 'your facebook app id',
'secret' => 'your facebook app secret code',
'allowSignedRequest' => false
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
if (isset($user_id)) {
try {
$likes = $facebook->api('/me/likes/your_facebook_page_id_here', 'GET');
if (!empty($likes['data'])) // if user has liked the page then $likes['data'] wont be empty otherwise it will be empty
{
echo 'Thank you for liking our fan page!';
// you can write some custom code here to award users some points or some badge
}
else {
echo 'You have not liked our fan page! Like it now:';
?>
<iframe src="//www.facebook.com/plugins/likebox.php?href=https%3A%2F%2Fwww.facebook.com%2Farkhitech&width&height=290&colorscheme=light&show_faces=true&header=true&stream=false&show_border=true&appId=1392604484339363" scrolling="no" frameborder="0" style="border:none; overflow:hidden; height:290px;" allowtransparency="true"></iframe> //replace this with your own Facebook like box code
<!--?php }
} catch (FacebookApiException $e) {
$login_url = $facebook-?-->getLoginUrl();
echo 'Please click here to login into your Facebook account.';
error_log($e->getType());
error_log($e->getMessage());
}
} else {
$login_url = $facebook->getLoginUrl();
echo 'Please lick here to login into your Facebook account';
}
?>
You would need to authorize the user with the user_likes permissions and then use the following API call:
/me/likes/[page-id]
As you can see, that´s the API call in your example code. Without telling us EXACTLY what errors you get, it´s impossible to help. But it does not matter anyway, because:
Incentivizing likes is not allowed, you can´t reward users in any way for liking your Page according to the platform policy.
You would need to get user_likes approved by Facebook, else it will only work for users with a role in the App. Facebook will definitely not approve it for something that is not allowed.
In other words: Not possible and not allowed.
I am new to facebook graph api
i have written a small application for posting comments on the user profile.
the following is the code:
<?php
require_once 'config.php';
/* Get a valid session */
$user= $facebook->getUser();
$me = null;
if($user) {
//Check if session is valid
$me = $facebook->api('/me');
}
if($me) {
echo 'User is logged in and has a valid session';
print_r($me);
$id=getfirstpostid($facebook->api('/me/posts'));
echo "id is ".$id;
$message="This is done programatically";
$comment_id = $facebook->api('/'.$id.'/comments','POST',
array('message' => $message,));
//$like_id = $facebook->api('/'.$id.'/likes', 'POST');
}
else {
//$login_url = $facebook->getLoginUrl();
//header("Location: {$login_url}");
echo 'Session expired or user has not logged in yet.
Redirecting...';
$login_url=$facebook->getLoginUrl(array('req_perms'=>'publish_actions,user_likes,offline_access,manage_pages,user_friends',));
echo '<script>top.location.href="'. $login_url .'";</script>';
}
function getfirstpostid($feeds)
{
return $feeds['data'][0]['id'];
}
?>
<?php
$logoutUrl = $facebook->getLogoutUrl(array('next' => 'http://distanceeducationhelpline.org/',));
?>
<a href='<?php echo $appBaseUrl?>about.php' target='_top'>
About Us</a>
<a href='http://www.9itech.com' target='_top'>External Link</a>
<br/>
<br/>
<a href ="#" onclick="top.location.href='<?php echo $logoutUrl; ?>';
return false;">Logout</a>
a. While creating the app ,i have not provided secure Canvas Url and accordingly the Canvas page does not show up in facebook.
is it due to the secure canvas url not being proper that blank page comes in the canvas page url.
b. when i am accessing the user profile the profile does not ask for permission to post on his profile even though i have given permissions .
i am not able to get the likes and post comments to work.kindly update if there is any problem in code.
do we need to get access tokens in this case or the code is proper.
thanks
I develop a Facebook application and I use PHP-sdk I use the algorithm described in the official documentation to allow user joining my app then I store his/her user_id in the database. My app uses publish_stream permissions so it publishes posts to the user's time line automatically.
I found that the application, always, does not able to publish posts for some of the registered users in the database (not all of them). I account this for they may be registered to the database before they give approval for the required permissions. The following is a blue print for the code that I use from the official documentation of Facebook api:
<?php
require_once('php-sdk/facebook.php');
$config = array(
'appId' => 'YOUR_APP_ID',
'secret' => 'YOUR_APP_SECRET',
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
?>
<html>
<head></head>
<body>
<?php
if($user_id) {
try {
/* $user_profile = $facebook->api('/me','GET');
echo "Name: " . $user_profile['name'];*/
//Here I run the code to save.
saveTheUserIdToRecordInTheDB($user_id);
//hypothetical function to save
} catch(FacebookApiException $e) {
// If the user is logged out, you can have a
// user ID even though the access token is invalid.
// In this case, we'll get an exception, so we'll
// just ask the user to login again here.
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
} else {
// No user, print a link for the user to login
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
}
?>
</body>
</html>
My, main, question is: If the user clicked on the login link, and then he/she does not continue for any reason (losing the internet connection, refusing giving the permissions to the app, etc), would it possible the hypothetical function saveTheUserIdToRecordInTheDB() to run and save that user's Facebook's ID to the database?
Or What's the explanation for there are some users are not posts publishable?
How are you supposed to deal with people who signed into Facebook a while ago. Come to your site you should them a continue link (because you detect that they are already logged into Facebook) and then on the page you direct them to you get this error.
Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user. thrown in /var/www/html/lib/base_facebook.php on line 1039
I don't understand how you are supposed to prevent this? Does this have something to do with the signed info that you give back to Facebook to get an access_token? Seems like this signed info can expire (it has an issued_at date). What is the correct way to handle this in your website's flow?
Are you expected to write code like this:
<?php
$user = $facebook->getUser();
try {
// attempt to do a call just to see if you are going to have this issue
$profile = $facebook->api('/me');
} catch (Exception $e) {
$user = false;
}
if ($user) { ?>
Begin
<?php } else { ?>
<fb:login-button scope="email" size="large">Connect</fb:login-button>
<?php } ?>
Instead of this:
<?php
$user = $facebook->getUser();
if ($user) { ?>
Begin
<?php } else { ?>
<fb:login-button scope="email" size="large">Connect</fb:login-button>
<?php } ?>
Getting the $user back from the Facebook SDK only seems to tell you there is a cookie. And not if that will actually work when you go to do the API calls.
UPDATE:
So my only problem with this method is ... when the user does have a cookie on my site, but the API call fails - I show them the connect button. User clicks the connect button, it quickly appears and disappears. Because it wasn't a true 'auth.login' that just occured, the user will not get sent to my start.php page via JavaScript redirect. How do others handle this? I'm stumped. Please tell me if there are other flaws with how I'm attempting to do this.
Try passing the access token to the API call that verifies that the user has authorized your application. Below is what I do, and it should help to alleviate the OAuthException you're getting.
$user = $facebook->getUser();
if($user) {
$access_token = $facebook->getAccessToken();
$params = array('access_token' => $access_token);
try {
$me = $facebook->api("/me", $params);
} catch(FacebookApiException $e) {
$user = null;
}
}
if($me) {
// proceed with authenticated user with an active access token.
}
Are you expected to write code like this:
Yes, you will have to handle the OAuthException.
But rather than using FBML login button, you can redirect the user to facebook login url for your application. You can get the url by using function getLoginUrl provided by Facebook PHP SDK. See getLoginUrl for more information.
For your email permissions you can use following array:
$params = array( 'scope' => 'email' );
Once successfully logged in, the user will be redirected back to the application page which redirected him to facebook login.
I got a problem regrading with my apps which is once I go to my apps, it sure will show me a login page instead of allow page?
it always display the login page 1st then only display allow page, I had tried other apps, if I am 1st time user, It sure will appear the allow page only, it did not show me the login page.
my question is how to I avoid my login page direct go to allow page?
here is my login page picture
here is my apps link
https://apps.facebook.com/christmas_testing/
here is my facebook php jdk api coding
<?php
$fbconfig['appid' ] = "XXXXXXXXXXXXX";
$fbconfig['secret'] = "XXXXXXXXXXXXX";
$fbconfig['baseUrl'] = "myserverlink";
$fbconfig['appBaseUrl'] = "http://apps.facebook.com/christmas_testing/";
if (isset($_GET['code'])){
header("Location: " . $fbconfig['appBaseUrl']);
exit;
}
if (isset($_GET['request_ids'])){
//user comes from invitation
//track them if you need
header("Location: " . $fbconfig['appBaseUrl']);
}
$user = null; //facebook user uid
try{
include_once "facebook.php";
}
catch(Exception $o){
echo '<pre>';
print_r($o);
echo '</pre>';
}
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => $fbconfig['appid'],
'secret' => $fbconfig['secret'],
'cookie' => true,
));
//Facebook Authentication part
$user = $facebook->getUser();
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => 'email,publish_stream,user_birthday,user_location,user_work_history,user_about_me,user_hometown'
)
);
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
//you should use error_log($e); instead of printing the info on browser
d($e); // d is a debug function defined at the end of this file
$user = null;
}
}
if (!$user) {
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}
//get user basic description
$userInfo = $facebook->api("/$user");
function d($d){
echo '<pre>';
print_r($d);
echo '</pre>';
}
?>
This is an effect of the new auth dialog features facebook recently rolled out.
There are two "allow/login" screens when using this new dialog :
Regular permissions such as - user_about_me, user_birthday, user_photos.
Extended permissions such as - read_insights, offline_access, publish_actions.
Each will display its own dialog. First the regular permissions will appear and after that the extended permissions will appear (if you have requested extended permissions)... This happens when the user first logs in, and possibly later if you want decide to add additional permissions to your app.
Taken from the Open Graph Beta › Auth Dialog
The updated Auth Dialog will display a set of user and friends permissions on the first dialog, and other extended permissions (if any) on a second dialog screen.
You can disable this feature and go back to the older auth dialog by changing the setting inside your app. Goto the advanced tab in the settings of your app and uncheck the Enhanced Auth Dialog check-box.
On the scope parameter, add 'offline_access' permission. With this,the user grant permission to the application even if the user is offline. The access_token that you will get is non-expiry.