Facebook PHP SDK empty album - facebook

I am working on a Facebook App that get the user Facebook albums and pictures. I all works fine but if the user has an album with no picture in it my app crash because it can't get an album cover and album pictures count. I've tried to escape the foreach that I use but with no success. It "iterate" only the albums prior to the empty one. Here is the raw code snippet:
<?php
$albums = $facebook->api("/me/albums");
foreach ($albums['data'] as $album) {
$album_id = $album['id'];
$album_cover = $album['cover_photo'];
$album_name = $album['name'];
$album_count = $album['count'];
$covers = $facebook->api("/" . $album_cover . "");
$source = $covers['source'];
?>
Some guidance and suggestions are more than welcomed.

Test to see if your album has pictures before getting any data:
$albums = $facebook->api("/me/albums");
foreach ($albums['data'] as $album) {
if (0 < $album['count'] {
$album_id = $album['id'];
...
}
}

Related

How can i save the facebook profile picture to my local folder using facebook graph api in codeigniter?

I have the sdk and facebook login is working fine. I am getting the user details without any problem and problem is only with the profile picture.
If you are geting user profile image then use following function
It work for me
$this->save_profile_photo($socialUser['image']);
function save_profile_photo($link) {
if ($link) {
$img = file_get_contents($link);
$imagename = "/profile" . . '-' . date('y-m-d-H-i-s') . '.jpg';
$file = base_url(PROFILE_IMAGE) . '/' . $imagename;
file_put_contents($file, $img);
}
}

Retrieving Facebook friends list

I am not able to get the Facebook friends list using the API. It was working fine earlier but not anymore. Here is the code I have used. Can someone help to validate the code. Thanks in advance.
<?php
require_once('PHPMailerAutoload.php');
require_once('PHPMailerConfig.php');
require_once("facebook/facebook.php");
$config = array();
$config['appId'] = '***************';
$config['secret'] = '*******************************';
$facebook = new Facebook($config);
$uid = $facebook->getUser();
if(!empty($_GET['error'])) {
echo "User cancelled the authentication with facebook";
echo "<script>
window.opener.location.href='merror.php';
window.close();
</script>";
exit;
}
if($uid) {
$url = $uid.'?fields=id,work,friends,email,picture.width(450).height(450)';
$user_profile = $facebook->api($url);
$friends = $facebook->api('/me/friends');
$d = '';
foreach($friends['data']as $val) {
$d .= $val['id'].',';
}
}
?>
Please read the docs before posting a question which has been answered here hundreds of times.
See
https://developers.facebook.com/docs/graph-api/reference/v2.3/user/friends#read
A user access token with user_friends permission is required to view the current person's friends.
This will only return any friends who have used (via Facebook Login) the app making the request.
If a friend of the person declines the user_friends permission, that friend will not show up in the friend list for this person.

Adding an additional field to login with AWD Facebook Wordpress Plugin

I am using AWD Facebook wordpress plugin to allow my visitors to login with their Facebook account information. When a visitor registers on my site I automatically create a new post that is titled with their username and includes their Facebook profile picture as the content. The code for that is below:
function my_create_page($user_id){
$fbuide = 0;
$the_user = get_userdata($user_id);
$new_user_name = $the_user->user_login;
$new_user_avatar = get_avatar($the_user->user_email);
global $AWD_facebook;
$fbuide = $AWD_facebook->uid;
$headers = get_headers('http://graph.facebook.com/' . $fbuide . '/picture?type=large',1);
if(isset($headers['Location'])) {
$url = $headers['Location']; // string
} else {
$url = false;
}
$my_avatar = "<img src='" . $url . "' class='avatar AWD_fbavatar' alt='" . $alt . "' height='" . $size . "' />";
$my_post = array();
$my_post['post_title'] = $new_user_name;
$my_post['post_type'] = 'post';
$my_post['post_content'] = $my_avatar;
$my_post['post_status'] = 'publish';
wp_insert_post( $my_post );
}
add_action('user_register', 'my_create_page');
What I am looking to accomplish is a bit different though. I also want to include a brief biography about the user (currently the post is simply their picture). So when a visitor logs in with AWD Facebook, their needs to be an additional field that allows the user to type in their bio. Then I would be able to grab that info from their user profile and include it in the post. Any ideas on how I can accomplish this? Is there a different way to do this?
I would recommend storing their Facebook picture as metadata and use the content area as their bio for the automatically generated post. So something like this should get you started:
$my_post = array(
'post_title'=>$new_user_name,
'post_type'=>'post',
'post_content'=>'',
'post_status'=>'publish'
);
if( $id = wp_insert_post( $my_post ) ){
update_post_meta($id, 'avatar', $url);
}
Then you can generate the loop like so:
if ( have_posts() ) : while ( have_posts() ) : the_post();
//... stuff here
$avatar = get_post_meta($post->ID, 'avatar', 'true');
the_content();
echo '<img class="avatar AWD_fbavatar" src="'.$avatar.'" alt="'.$alt.'" height="'.$size.'" />';
endwhile;endif;

posting images into users feed with source is not working

i am trying to create a fb app using the user profile pic. app will generate a new image that image should post to user feed. I am trying to use "source" but it is not working. This is not giving full image. It gives thumbnails instead of posting full image. Here is my code
$canvas = imagecreatetruecolor($crop_width,$crop_height);
imagecopy($canvas,$currentimage,0,0,0,0,$crop_width,$crop_height);
$test = $data['user_id'].'test.jpg';
imagejpeg($canvas, $test,100);
$new = imagecreatetruecolor(150,150);
imagecopyresized($new,$canvas,0,0,0,0,150,150,$w,$crop_height);
$resize1 = $data['user_id'].'resize1.jpg';
imagejpeg($new, $resize1 ,100);
$source1 = 'black.jpg';
$background = imagecreatefromjpeg($source1);
$resize = imagecreatefromjpeg($resize1);
imagecopy($background,$resize,100,250,0,0,150,150);
$output = $data['user_id'].'output.jpg';
imagejpeg($background,$output,100);
echo '<div align="center"><img src="'.$output.'"></div>';
unlink($test);
unlink($resize1);
$access_token = "ABCDEFGHIJKLM";
$graph_url ="https://graph.facebook.com/me/feed?access_token=".$access_token."&method=post&source=http://www.Google.es/images/srpr/nav_logo39.png&message=logo";
$user_permissions = json_decode(file_get_contents($graph_url));
print_r($user_permissions);
Please help me this solves me to create a perfect fb app.

Fetching friends' birthdays from facebook profile

I want to fetch 'birthdays' of users, and their friends on my website, from their facebook profiles (with their facebook credentials supplied).
Is their a feature in Facebook API/Connect that I can use to fetch these details from facebook as possible on Native Facebook Apps using Facebook API.
I want to store this data in my DB, and users will be asked for their facebook credentials and consent before this is done.
Read the api documentation things like this are easily done. You can do it like this:
$facebook = new Facebook( $apikey, $secret );
$uid = $facebook->require_login();
$friends = $facebook->api_client->friends_get(); // $friends is an array holding the user ids of your friends
foreach( $friends as $f ) {
$data = $facebook->api_client->fql_query( "SELECT birthday_date FROM user WHERE uid=$f" );
// $data[0] is an array with 'birthday_date' => "02/29/1904"
// see api documentation for other fields and do a print_r
}
So recently I wanted to check my friends to see if any of them had their birthday for the current day. Using FQL this is super easy and I encourage you to explore FQL more because it will yield a more efficient solution than, say, what Pierre kindly offered. Here is a small snippet of the code:
$friends = $facebook->api_client->friends_get();
$uids = "";
foreach($friends as $f) {
$uids .= "uid=$f OR ";
}
$query_uids = substr($uids,0,strlen($query_uids)-4);
date_default_timezone_set('UTC');
$current_date = date("m/d");
echo "<br />Searching for birthdays for the given month/day: $current_date<br />";
$data = $facebook->api_client->fql_query( "SELECT name, uid FROM user WHERE ( ($query_uids) AND strpos(birthday_date,'$current_date') >= 0 )" );
if(count($data) > 0) {
foreach($data as $d) {
print_r($d);
}
} else {
echo "<br />No Birthdays Today<br />";
}
require_once('facebook-platform/client/facebook.php');
$facebook = new Facebook(API_KEY, SECRET);
$facebook->require_login();
function getInfo($user_list, $fields)
{
try
{
$u = $facebook->api_client->users_getInfo($user_list, $fields);
return $u;
}
catch (FacebookRestClientException $e)
{
echo $e->getCode() . ' ' . $e->getMessage();
}
}
function getFriendsBirthdays($user_id)
{
$f = $_REQUEST['fb_sig_friends'];
$f = explode(',', $f);
$birthdays = array();
foreach($f as $friend_id)
{
$birthdays[] = getInfo($friend_id, 'birthday');
}
return $birthdays;
}
Do something like that or use the Batch API to do multiple calls at once. Check the Facebook API.
You could fetch it via the API, but Facebook's terms strictly forbid you from storing anything other than their user ID in your database - see the developer wiki for details. You will need to query the API each time.