Publishing comments on a user post wtih graph api - facebook

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

Related

setting up facebook api login with php authorization

Currently I have the facebook api working. I have included scope string to grab extra permissions.
I am lacking the ability to authorize access to restricted pages on my site. How can I set it up so when facebook logs the user in they are also logged in to my site?
<?php
include 'src/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => '****************',
'secret' => '****************',
'cookie' => true
));
// 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.
$user_profile = $facebook->api('/me');
} 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();
echo "<a href='$logoutUrl'>Logout</a>";
} else {
$statusUrl = $facebook->getLoginStatusUrl();
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'user_checkins, user_location, friends_checkins')
);
echo "<a href='$loginUrl'>Login</a>";
}
?>
<h3>Welcome to your profile</h3>
<?php
echo $user_profile['first_name'];
echo ' <span> </span> ';
echo $user_profile['last_name'];
?>
This is the basic php api from facebook.
There are endless ways to do this, but you should just be able to place the private content inside of an IF block checking for the logged in Facebook user:
<?php if ($user): // Show the private content ?>
<h3>Welcome to your profile</h3>
<?php
echo $user_profile['first_name'];
echo ' <span> </span> ';
echo $user_profile['last_name'];
?>
<?php endif; ?>

Facebook Canvas App Login for new Users

i have created a facebook app and it works well for new users if they start the app with the direct server link (https://www.mydomain.com/app/index.php) and use the login button.
But if they try to use the app within facebook (https://apps.facebook.com/myapp) as a canvas app(for the first time) then they see the "landing" page of my app and the log in button. But if they click on it, an errormessage appears, that the content of the iframe couldn't be displayed. If they start the app in a new tab, then the login process works. (see the error below, sorry it's german, maybe you are familiar with this type of error message?)
It would be desirable if the new user could use the login link in the canvas app.
How can this be achieved? My index.php looks like this: (i am using the facebook SDK)
<?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'];
} catch(FacebookApiException $e) {
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
} else {
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
}
?>
</body>
</html>

When the user agree and give my Facebook app permisions?

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?

Facebook connect trouble

I've been struggeling with the facebook connect for a few days now, I'm only using the PHP SDK downloaded from facebook.
I first used some custom code, but after that failed I just copied the code on facebook, but this still fails.
Is there anything I do wrong? I've created an app on facebook, added the right domain (I get redirected correctly).
Once people login, this is the URL they get redirected to:
http://www.mysite.com/index.php?state=33e35654a84559d246c152ed10e8150b&code=AQBGxCMu4vgbw5HgU8EIoyq8rhuHaKvtJQR-VbPMVH8bd2JMIRcxojqJ-l7XrjIdG9TNN05el14Jv8isbHbUWj9so-CdhaEqj7tLR-Rj6-JaOTA7QErrpfN_0XQN1CGCmvmTL6ZtoUupgkVwkzq_CWDT9lSoDPvNHu2F67Jqlsi2DfQZGE1J7pDzujBoSoJhDhs#_=_
The code I copied from facebook:
<?
// Remember to copy files from the SDK's src/ directory to a
// directory in your application on the server, such as php-sdk/
require_once('classes/facebook/facebook.php');
$config = array(
'appId' => 'appID here',
'secret' => 'secret here',
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
?>
<html>
<head></head>
<body>
<?
if($user_id) {
// We have a user ID, so probably a logged in user.
// If not, we'll get an exception, which we handle below.
try {
$user_profile = $facebook->api('/me','GET');
echo "Name: " . $user_profile['name'];
} 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>
It turns out it had something to do with the certificate, after some looking around I found out that changing
if (curl_errno($ch) == 60) {
// CURLE_SSL_CACERT
to
if (curl_errno($ch) == 60 || curl_errno($ch) == 77) {
// CURLE_SSL_CACERT
solves the problem

facebook : load the wall content of a facebook page in a website

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.