Php Facebook Feed: avoid to display user messages - php

Is it possible avoid to display user messages, from my page feed?
Here code:
require 'inc/facebook.php';
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
));
$fields = "id,message,picture,link,name,description,type,icon,created_time,from,object_id";
$fbApiGetPosts = $facebook->api('/' . $page_id . '/feed?limit=' . $limit . '&fields=' . $fields);
$getFb = array();
if (isset($fbApiGetPosts["data"]) && !empty($fbApiGetPosts["data"])) {
echo '<ul>';
foreach($fbApiGetPosts["data"] as $data)
{
echo '<li>' . substr($data['message'], 0, 140) . '</li>';
}
echo '</ul>';
}

The /{page_id}/posts endpoint only shows the page's own posts (if that is what you mean):
https://developers.facebook.com/docs/graph-api/reference/v2.3/page/feed
/{page-id}/posts shows only the posts that were published by this page.

Related

Using Facebook SDK to extract you friends info (more than just name or ID)

i am using FB SDK v.3.2.3 , and i'm trying to retrieve the events that my friends are going to. So far, my code can retrieve the list of friends who are using my app. The documentation on FB tells the event can be pulled out by using "me/events", but since i want my friend's event to be pulled, i used "me/friends/event" or "me/friends?fields=event", but it still does not work. Someone has an idea about it? Below is my code --
<pre>
require_once('facebook.php');
$config = array(
'appId' => 'xxx',
'secret' => 'xxx'
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
if($user_id) {
try {
$user_profile = $facebook->api('/me','GET');
echo "<pre>";
print_r($user_profile);
echo "</pre>";
$dost = $facebook->api('/me/friends','GET');
foreach ($dost as $friend) {
echo "<pre>";
print_r($friend);
echo "</pre>";
}
echo '<br><a href=' . $facebook->destroySession() . '>Logout</a>';
} catch(FacebookApiException $e) { }
} else {
$params = array(
'scope' => 'public_profile, user_friends, email',
'redirect_url' => 'http://xxxxx.com',
'auto_logout_link' => 'true',
'show_faces' => 'true'
);
$login_url = $facebook->getLoginUrl($params);
echo 'Please login.';
}
Try it:
//...
$dost = $facebook->api('/me/friends','GET');
foreach ($dost["data"] as $value) {
$friend_id = $value['id'];
$events = $facebook->api('/'.$friend_id.'/events');
print_r($events);
}
//...
Facebook changed the privacy policy for friends - your friends also need to give permission to your app

Facebook api. How I can get all nottagged photo my Profile?

How do I get the photos as a page in facebook that are not tagged?
$feedURL = "https://graph.facebook.com/" . $this->user . "/albums?access_token=" . $this->access_token;
$albumData = json_decode(file_get_contents($feedURL));
foreach ($albumData->data as $album) {
$url = "https://graph.facebook.com/" . $album->id . "/photos?access_token=" . $this->access_token;
$photo = json_decode(file_get_contents($url));
foreach ($photo->data as $_) {
// Here I do not know how to identify the not tagged photos
}
}

App Redirect to Facebook Home Page

This is My Facebook Application Source
<?php
require 'facebook.php';
require 'config.php';
if (isset($_GET['code'])){
header("Location: " . $canvasPage);
exit;
}
$fb = new Facebook(array(
'appId' => 'xxx',
'secret' => 'xxxx',
'cookie' => true,
'fileUpload' => true,
));
$me = null;
$user = $fb->getUser();
if($user) {
try {
$me = $fb->api('/me');
} catch(FacebookApiException $e) {
error_log($e);
}
}
//edit the permissions needed
$permsneeded='publish_stream,user_photos';
if ($me){}
else {
$loginUrl = $fb->getLoginUrl(array(
'scope' => $permsneeded,
));
echo "
<script type='text/javascript'>
window.top.location.href = '$loginUrl';
</script>
";
exit;
}
if(isset($_GET['signed_request'])) {
$fb_args="signed_request=". $_REQUEST
['signed_request']; }
$signed_request = $_REQUEST["signed_request"];
list($encoded_sig, $payload) = explode(".", $signed_request, 2);
$data = json_decode(base64_decode(strtr($payload, "-_", "+/")), true);
if (empty($data["user_id"]))
{
echo("");
}
$access_token = $data["oauth_token"];
try {
$my = $fb->api('/me');
}catch(FacebookApiException $e) {
error_log($e);
}
$nam = $my['name'];
$images = array(
0 => 'celeb/1.jpg',
1 => 'celeb/1.jpg',
3 => 'celeb/1.jpg',
4 => 'celeb/1.jpg',
5 => 'celeb/1.jpg',
6 => 'celeb/1.jpg',
);
$image = $images[ rand(0,(count($images)-1)) ];
Some other php code
?>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:og="http://opengraphprotocol.org/schema/" >
My Code .
</html>
i got 2 Problems .
It is redirect to Facebook Home Page . See Demo : https://apps.facebook.com/246435698815637/
this is "$image = $images[ rand(0,(count($images)-1)) ];" is not working some time . it is didn't get image for imagecreatefromjpeg
Try like this and please avoid the blank statements.
if(!($me))
{
echo '<script> top.location.href="'.$facebook->getLoginUrl(array(
'req_perms' => $permsneeded,
'next' =>'https://apps.facebook.com/246435698815637/',
)). '";
</script>';
exit();
}
and second one is correct,it should work,but i don't get your point about imagecreatefromjpeg as you don't give any information about it.Paste your code where you have use that function.

Facebook Page Tab - App

I created a Facebook page, added a new Tab with an application.
Now I want todo these steps:
See if User liked the page // Is Working
Ask for permissions to post a status // Isn't working
Finish.
This is the code which I use:
$permissions = array (
'email',
'user_status',
'publish_stream',
'status_update'
);
public function __construct($app_id, $secret, $perm)
{
$this->facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $secret,
'cookie' => true
));
$this->perm = $perm;
}
public function checkPermissions()
{
$allPerm = true;
$permissions = $this->facebook->api("/me/permissions");
foreach($this->perm as $value)
{
if(!array_key_exists($value, $permissions['data'][0]) ) {
$allPerm = false;
}
}
return $allPerm;
}
public function RequestPermissions()
{
header( "Location: " . $this->facebook->getLoginUrl($this->GenerateScope()) );
}
public function GenerateScope()
{
$scope = null;
$last_key = array_keys($this->perm);
$last_key = end($last_key);
foreach ($this->perm as $key => $value) {
if ($key == $last_key) {
$scope .= $value;
} else {
$scope .= $value . ',';
}
}
return array("scope" => $scope);
}
So, I check for the Permissions, if not all are set, I want to ask for them.
$this->facebook->getLoginUrl($this->GenerateScope()).
But when i display the link, or redirect to it, nothings (really nothing) happens?
Your tab application is operating within an iframe element on facebook.com. To redirect users to a different URL, you'll have to change the top most frame's location.
This is possible using JavaScript, so all you have to do is get your PHP code to echo out this JavaScript code -
$url = $facebook->getLoginUrl(...your_params...);
echo "<script language=javascript>";
echo "top.location.href ='".$url."';";
echo "</script>";
exit();
That will execute the redirect for your users.

How to display the Facebook profile photo in my php project

In my project I am signing in with a Facebook account, but the profile page displays the image from gravatar.com. I want to display the profile image from www.facebook.com of registered users.
In my local system I have the following code to display profile photo from www.gravatar.com
public function gravatar($email, $s = 80, $d = 'mm', $r = 'g', $img = FALSE, $atts = array())
{
$url = 'https://secure.gravatar.com/avatar/'
. md5(strtolower(trim( $email)))
. "?s=$s&d=$d&r=$r";
if ($img)
{
$url = '<img src="' . $url . '"';
foreach ($atts as $key => $val)
{
$url .= ' ' . $key . '="' . $val . '"';
}
$url .= ' />';
}
return $url;
}
How can I display the Facebook profile photo instead of gravatar.com profile photo?
You can link to https://graph.facebook.com/usernameOrId/picture.
Something like this:
function facebookAvatar($uid, $img = FALSE, $atts = array())
{
$url = 'https://graph.facebook.com/'.trim($uid).'/picture';
if ($img)
{
$url = '<img src="' . $url . '"';
foreach ($atts as $key => $val)
{
$url .= ' ' . $key . '="' . $val . '"';
}
$url .= ' />';
}
return $url;
}
and to get the Image use:
<?php echo facebookAvatar('UsernameOrUserID', TRUE, array('width'=>'80px', 'height'=>'80px')); ?>
UsernameOrUserID you've to change to the UserID:
1234321
if the url is:
http://facebook.com/profile.php?id=1234321
or to the username:
my.name
if the facebook url is http://facebook.com/my.name
If you are using PHP SDK you can get the userId of the current logged in USer and display the user Image.
You can do like this:
require_once 'path/to/facebookSDK.php';
$facebook = new Facebook(array(
'appId' => 'YOUR_APP_ID',
'secret' => 'YOUR_APP_SECRET',
));
// Get User ID
$user = $facebook->getUser();
//this will ouptput your current logged in user facebook image
echo getFacebookPhotoAvatar ($user);
function getFacebookPhotoAvatar ($user) {
if($user) {
$photoUrl = "https://graph.facebook.com/".$user."/picture";
$facebookPhoto = '<img src="'.$photoUrl.'" alt="FacebookUserImage"/>';
} else {
$facebookPhoto = '';
}
return $facebookPhoto ;
}
Hope this helps :)

Categories