Facebook API - Get Comment with php sdk error? - php

I have tried to get facebook comments, but always displays {"data":[]} , but in tools graph explorer, work perfectly
i'm using php-sdk
this my code
<?php
require 'inc/facebook.php';
$facebook = new Facebook(array(
'appId' => 'xxx',
'secret' => 'xxx',
'cookie' => true,
));
$user = $facebook->getUser();
if($user) {
$get = $facebook->api("/807642829246790/comments","GET");
print_r($get);
} else {
header("Location:".$facebook->getLoginUrl());
}
?>
so, what wrongs above code ?

This is because you are using the app token instead of user token. Try copying the token directly from graph explorer and use it to instantiate a new FB instance. Then retrieve comments from that instance. You would see the results as seen in graph explorer.

Related

is it possible to get my fb wall entries in my website database ?

i want to have my or someone else facebook wall entries (only posts , no comment or pictures ) in my website database ..... i've been reading around but i couldn't find much .
so i wonder if it is possible at all ? i've gone as far as login in to app and retriving access token
require '../src/facebook.php';
$facebook = new Facebook(array(
'appId' => '3333',
'secret' => 'xxxxx',
));
$user = $facebook->getUser();
if ($user) {
$user_profile = $facebook->api('/me');
$access_token = $_SESSION['fb_284654551659478_access_token'] ;
}
what now ? i cant find anything in fb document about retriving user entries
Checkout Facebook Graph Explorer: https://developers.facebook.com/tools/explorer/
Use: me?fields=posts
You can try using graph API,
https://graph.facebook.com/{SOME_USER_NAME}/feed?access_token={SOME_ACCESS_TOKEN}

Get/store Facebook access token from REQUEST in PHP SDK 3.0

Let's say I get a valid access token from an external application that does Facebook Connect and sends me the token on GET.
Is it normal/good for me to get that token and save it in the $facebook = new Facebook(...); object? Something like:
$facebook = new Facebook(array(
'appId' => $this->app_id,
'secret' => $this->app_secret,
));
$token = $facebook->getAccessToken();
if(empty($token) && isset($_REQUEST['access_token']))
$facebook->setAccessToken($_REQUEST['access_token']);
$userID = $facebook->getUser();
if(!$userID)
{
return false;
}
return $facebook;
And if this is OK to do, does the SDK also store this value in the SESSION or COOKIE array, or do I need to pass it to every script that requires it?
Found out how this works. The SDK doesn't set my SESSION when calling setAccessToken(), I have to set it manually and it will work.

Facebook and php - how to post content from my site to my facebook wall?

I'm very confused with this facebook api.
I've facebook username and password in my database, I want to sign and post content from my site to my facebook wall. how do I do it?
I've downloaded the facebook-php-sdk, just need to be able to include the "post" part in my php file to make things happen, I've this code at the moment
require_once ('facebook.php');
$app_id = "MY_ID";
$app_secret = "MY_SECRET";
$facebook = new Facebook(array('appId' => $app_id, 'secret' => $app_secret, 'cookie' => true));
if(is_null($facebook->getUser())) {
header("Location:{$facebook->getLoginUrl(array('req_perms' => 'user_status,publish_stream,user_photos'))}");
exit;
}
$status = $facebook->api('/me/feed', 'POST', array('message' => utf8_encode($my_message)));
with this I just get a internal server error, nothing more to have an idea of what I'm doing wrong...
even with var_dump($status) I get nothing
Thanks
You cannot log in as a user on the Facebook Graph API. You should never be asking for any user's Facebook password in the first place, which is why Facebook do not allow this.
The API allows you to log in as an 'app', which can be granted permissions by Facebook users. You will need to create a Facebook app at https://www.facebook.com/developers/apps.php.
Once this is done, you can prompt a user to post on their wall or use the Graph API using http://developers.facebook.com/docs/reference/dialogs/feed/
Firstly try (i hope you entered your $app_id and $app_secret):
require_once ('facebook.php');
$app_id = "MY_ID";
$app_secret = "MY_SECRET";
$facebook = new Facebook(array('appId' => $app_id, 'secret' => $app_secret, 'cookie' => true));
$user = $facebook->getUser();
var_export($user);
You should se data if you're logged in properly.
Than you should maybe look at this page for an example Facebook Graph API PHP SDK posting on page as page

Issues getting the Facebook PHP SDK to work on my application

I was making a facebook application in which i have to show news feeds of the user who is using it. I am using graph API to do this. The problem is that its not getting me feeds.
I used it to show friends like this:
$friends = $facebook->api('/me/friends');
and it is working fine.
For news feeds i use this:
$feeds = $facebook->api('/me/home');
it shows me an error:
Fatal error: Uncaught IDInvalidException: Invalid id: 0 thrown in
/home/content/58/6774358/html/test/src/facebook.php on line 560
when i try to get Profile feed (Wall) by using this:
$feeds = $facebook->api('/me/feed');
it shows me an empty array.
These API calls are showing me results in the graph API page but don't know why not working in my application.Can any one help me please..
My full code is as follows
require_once 'src/facebook.php';
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => 'xxxxx',
'secret' => 'xxxxx',
'cookie' => true,
));
$session = $facebook->getSession();
$fbme = null;
// Session based graph API call.
if (!empty($session)){
$fbme = $facebook->api('/me');
}
if ($fbme) {
$logoutUrl = $facebook->getLogoutUrl();
echo 'Logout';
}else{
$loginUrl = $facebook->getLoginUrl();
echo 'Logout';
}
$friends = $facebook->api('/me/friends?access_token='.$session["access_token"]);
$feeds = $facebook->api('/me/feed?access_token='.$session["access_token"]);
print('<pre>Herere:');print_r($feeds);die;
Did you ask for the read_stream permission during authentication?
What type of Authentication / "Allow" process did you go through?
JavaScript SDK? Facebook PHP SDK? XFBML Login Button?
EDIT- Here are helpful link that will get you started up:
Facebook PHP SDK
Authentication Process
Building Apps on Facebook.com
These are all official docs in Facebook and github.
EDIT: Follow this step by step:
From your original code, look for:
$loginUrl = $facebook->getLoginUrl();
Change it to:
$loginUrl = $facebook->getLoginUrl(array('req_perms'=>'read_stream'));
Uninstall your application first from your account:
http://www.facebook.com/settings/?tab=applications
Then try it again to show new Allow pop-up
EDIT:
It's also about the arrangement of code in the if statements. Use this code:
<?php
require_once 'src/facebook.php';
$session = $facebook->getSession();
$fbme = null;
if($session){
$fbme = $facebook->api('/me');
$friends = $facebook->api('/me/friends');
$feeds = $facebook->api('/me/feed');
$logoutUrl = $facebook->getLogoutUrl();
echo 'Logout';
echo "<pre>".print_r($feeds,TRUE)."</pre>";
}else{
$loginUrl = $facebook->getLoginUrl(array('req_perms'=>'read_stream','canvas'=>1,'fbconnect'=>0));
echo '<script> top.location.href="'.$loginUrl.'"; </script>>';
}
Uninstall your app again from
http://www.facebook.com/settings/?tab=applications
and re-open the application
The Facebook PHP SDK should handle your access token for you, you don't need to append it to your graph API endpoint in you Facebook::api() call.
As #dragonjet pointed out, you need to request the read_stream extended permission from your FB User in order to get access to their feed. Though, the exception you pasted doesn't really match that kind of problem, and your request to /me/home doesn't throw a similar exception (or one about not having access).
I still think this is a permissions issue, so start here for trying to fix it. Here's an example of how to request the appropriate permission.
$facebook = new Facebook(array(
'appId' => FB_APP_ID, //put your FB APP ID here
'secret' => FB_APP_SECRET, //put your FB APP SECRET KEY here
'cookie' => true
));
$session = $facebook->getSession();
if ($session)
{
//check to see if we have friends_birthday permission
$perms = $facebook->api('/me/permissions');
}
//we do this to see if the user is logged & installed
if (empty($session) || empty($perms['read_stream']))
{
//get url to oauth endpoint for install/login
$loginUrl = $facebook->getLoginUrl(array(
//put the URL to this page, relative to the FB canvas
//(apps.facebook.com) here vvv
'next' => 'http://apps.facebook.com/path_to_your_app/index.php',
'req_perms' => 'read_stream'
));
//use javascript to redirect. the oauth endpoint cant be loaded in an
//iframe, so we have to bust out of the iframe and load it in the browser
//and tell the oauth endpoint to come back to the fb canvas location
echo "<script>window.top.location='{$loginUrl}';</script>";
exit;
}
print_r($facebook->api('/me/home'));
print_r($facebook->api('/me/feed'));

Facebook Extended permissions in canvas via php

I'm working on my new project, Woobook. I used the Old REST API until now but i should learn to work with latest one (with OAuth). First i want to get extended permissions but the attached code redirect me to a wrong page (instead of permissions dialog directly).
My app link: http://apps.facebook.com/woobook/
and the source code:
<?php
// Facebook API inc
require_once "inc/facebook.php";
// API init
$cid = "162513840463126";
$asi = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$facebook = new Facebook(array(
'appId' => $cid,
'secret' => $asi,
'cookie' => true,
));
// Session
$session = $facebook->getSession();
if(!$facebook->getUser()) {
header("Location:".$facebook->getLoginUrl(array("next" => "http://apps.facebook.com/woobook/", "canvas" => 1, "req_perms" => "user_status,publish_stream,user_photos"))."");
exit;
}
?>
You're using an iframe app, which means you need to do this a little differently.
See my answer here

Categories