Facebook SDK photos returns blank array - php

I am using this to retrieve user photos however it always print_r's a blank array:
$facebook = new Facebook(array(
'appId' => 'XXX',
'secret' => 'XXX',
'cookie' => true,
));
if ($facebook->getSession()) {
try {
$profile = $facebook->api('/me/photos');
echo "<pre>";
print_r($profile);
} catch (FacebookApiException $e) {
echo $e->getMessage();
exit();
}
}
else {
$login_url = $facebook->getLoginUrl(array(
'next' => 'XXX',
'cancel_url' => 'XXX',
'req_perms' => 'user_about_me,user_location,user_hometown,user_birthday,user_interests,user_likes,user_photos'
));
echo 'Click here to login';
}
If I use:
$profile = $facebook->api('/me');
It print_r's an array full of data but not the photo information I am looking for.
Any ideas?

I was having the same problem. My solution :
$facebook->api('/me/albums?access_token=' . $session['access_token']);
Hope It's not too late !

When you get the facebook session you also receive an access token. You have to use it to make valid api requests. Everything that's not public info require that token. Example of usage:
$session = $facebook->getSession();
// var_dump($session) to see it's content
$facebook->api('/me/photos?access_token=' . $session['access_token']);
If doing this you still get empty data, maybe that user does not have any photos.
Good luck!

Try https://graph.facebook.com/$album_id/photos
I am not sure why me/photos does return empty array. Probably it means something else, may be the photos where I am tagged or some other thing. For me too, it returns empty array consistently. but when I try to access graph.facebook.com/$album_id/photos then I get the array of all the photos in that album..

Related

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.

Facebook Php SDK - getUser() return always 0

I read all threads on this forum but I can't find my solution..
I use the last Php SDK by facebook : v3.2.2 .
So I used this:
//start fb
$facebook = new \Facebook(array(
'appId' => $fb_app_id,
'secret' => $fb_app_secret,
'cookie' => true,
));
// get id user-fb
$fbUser = $facebook->getUser();
if ($fbUser) {
try {
$fbUserProfile = $facebook->api('/me'); // take user-infos
$friends = $facebook->api('/me/friends'); // take his friend
} catch (FacebookApiException $e) {
error_log($e);
$fbUser = null;
}
}
if ($fbUser) {
$logoutUrl = $facebook->getLogoutUrl(array( 'next' => ('my-url') ));
} else {
$loginUrl = $facebook->getLoginUrl(array(
'canvas' => 1,
'fbconnect' => 0,
'scope' => 'email,offline_access,publish_actions',
'req_perms' => 'publish_stream,email',
'redirect_uri' => 'my-redirect-url'
));
}
So I don't understand what is wrong..
When I test it, first time I can click on the loginUrl and get authorization . but when I try to getUser again, the function $facebook->getUser() return 0 again and I got the loginUrl again but when I click on the link, I'm just redirect on the callback url..
I'm not sure if the application facebook need special parameters for this. My app_id and app_secret are 100% sure.
I solved this issue using $facebook->getLoginUrl(array(
'scope' => 'public_profile',
'redirect_uri' => 'http://www.your_domain/current_page.php'
));
where 'http://www.your_domain/current_page.php' is the same page where i am running this code
N.B using Facebook php sdk 3.2.3
(Question answered in the comments. See Question with no answers, but issue solved in the comments (or extended in chat) )
The OP wrote:
OK I solved the error: the callback url was wrong, I needed to call the current url and not a special one.

Login using SDK 3.1, $facebook->getUser() is zero?

So I've setup the basic example code the SDK suggests.
I'm having such a problem, well many problems as I'm sure you all are too.
I've used both the session approach with SDK 2.12, my current problem is, even though I'm passing the getLoginUrl a redirect_uri, it ignores this and redirects it to my base_domain +
'{"session_key":"badsdsadsdsdasdsd.0-1268121831","uid":"badsdsadsdsdasdsd","expires":0,"secret":"730aa9badsdsadsdsdasdsd","base_domain":"slumber-roo.co.uk","access_token":"badsdsadsdsdasdsdEZBZAZAvdRAZC6PwPeBZBYMZD","sig":"badsdsadsdsdasdsdd761385"}'
Obviously filled in with keyboard smash.
Now, my attempt with SDK 3.1 is weird. I have a valid getUser, but my colleague doesn't and gets stuck in the infinite loop.
I do understand the differences between getSession and getUser with SDK 2 and 3 respectively.
Here is my code:
<?php
$user = null; //facebook user uid
try{
include_once "facebook.php";
}
catch(Exception $o){
error_log($o);
}
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => $fbconfig['appid'],
'secret' => $fbconfig['secret'],
'cookie' => true
));
//Facebook Authentication part
$session = $facebook->getSession();
if ($session) {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
print_r($me);
} catch (FacebookApiException $e) {
error_log($e);
}
} else {
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => 'email,offline_access,publish_stream,user_hometown',
'redirect_uri' => 'http://www.facebook.com/pages/CharlesTestPage/225802194155435?sk=app_252946408094785'
)
);
exit('<script> top.location.href="'.$loginUrl.'"; </script>');
}
function d($d){
echo '<pre>';
print_r($d);
echo '</pre>';
}
?>
Oh, in addition, I'm running this app solely inside a tab.
Thanks in advance.
Is the application in sandbox mode? Have you added your collegue as an ADMIN || DEVELOPER || TESTER in the app config?
I just got that this morning, my project manager was in an infinite loop, added her as tester in the app but she hadn't accepted the pending invite and was looping.

Facebook API (Via PHP): Post to Feed?

I've got the basic idea covered (I think), but can't seem to successfully post anything using Facbook's provided PHP class. Given this example, can anyone see anything that might be the problem?
// Initialize Facebook API
$fb = new Facebook(array(
'appId' => FACEBOOK_APP_ID,
'secret' => FACEBOOK_SECRET,
'cookie' => true
));
if($fb->getSession()) {
$res = $fb->api('/me/feed/', 'post', array(
'message' => 'test message',
'description' => 'testing'
));
}
var_dump($res);
When I output the contents of $res I get a face-palming NULL. Any ideas? If the above looks correct, can anyone suggest some things I might try to debug this?
In your app, when you check to see if the user is logged into Facebook, make sure you are confirming that your app requested the 'special privilege' of posting to the user's wall. You can do this by modifying your code as follows:
$url = $this->facebook->getLoginUrl(array('canvas' => 1, 'fbconnect' => 0, 'req_perms' => 'user_status,publish_stream'));
Note the req_perms key being set to a list of values including 'publish_stream'. If you don't have this privilege the feed call will fail.
Also whether this is really the problem or not, you can wrap your Facebook requests with a try-catch block to catch FacebookApiException type exceptions to get more information when you have errors. For example:
try {
$uid = $this->facebook->getUser();
$me = $this->facebook->api('/me');
} catch (FacebookApiException $e) {
print_r($e);
}

Categories