Facebook - retrieve user info using ID - facebook

In my database I have facebook ID-s of users of my App.
Now I need to retrieve these users in PHP.
I use this:
$json=file_get_contents("http://graph.facebook.com/UID");
$fdata=json_decode($json);
$fname=$fdata->name;
echo '<img src="https://graph.facebook.com/'.$row['uid'].'/picture">';
echo '$fname';
I need to put this code into WHILE.. I already retrieve users photos using UID, but I also need names. As I understand I should write something Like this:
while($row = mysql_fetch_array($result)){
$json=file_get_contents("http://graph.facebook.com/$row['uid']");
$fdata=json_decode($json);
$fname=$fdata->name;
echo $row['uid'];
echo '<img src="https://graph.facebook.com/'.$row['uid'].'/picture">';
echo "<br>";
}
But it doesn't work. What I'm doing wrong??
Thanks in advance!

Use the official PHP SDK for Facebook.
https://github.com/facebook/php-sdk/tree/master/src
Then call:
$fb = new Facebook(array(
'appId' => <app_id>,
'secret' => <app_secret>,
'cookie' => true
));
Then use the api, like:
$varfordata = $fb->api();
A good site for searching: http://fbdevwiki.com/wiki/

Related

Access non-friends profile updates using php

Is it possible to access profile updates of a facebook user who is not in your(application authorizer) friend list or like-list?
For example:
http://www.facebook.com/zuck
or
http://www.facebook.com/mashable
Is it possible using facebook api?
It depends on their privacy settings
<?php
require 'src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'xxx',
'secret' => 'xxx',
));
$username = isset($_GET['username'])?$_GET['username']:"johnbevere.page";
$profile = $facebook->api('/'.$username.'/posts');
?>
<br/><br/><img src="https://graph.facebook.com/<?=$username?>/picture">
<?php
foreach( $profile['data'] as $data ){
if($data['message']){
echo $data['message']."<br><hr><br>";
}else{
echo $data['story']."<br><hr><br>";
}
}
?>
Success:
Page updates
User timeline Activity Log
Failed:
User timeline Feed
It is not possible to scrap the public feed of random profile using facebook API

Facebook PHP SDK: getAccessToken not correct on refresh

On my facebook app, I'm logging a user in using the Javascript SDK.
If OK, I redirect him to this PHP page (designed to illustrate my problem):
test.php:
<?php
include('config.php');
require('phpsdk.php');
$access_token = $facebook->getAccessToken();
echo $access_token;
echo "<br /><br />";
$user = $facebook->getUser();
echo $user;
?>
(phpsdk.php handels the PHP SDK initiation)
Accessing test.php the FIRST time, returns a valid access token and the ID of the logged user.
However, when I refresh the page, the access token becomes of the type appID|appSecret.
The user is still logged in: getUser still returns the correct user ID.
Anyone knows what's causing this?
Clearing the Facebook Session with php sdk in redirect when user logs out.
Logout.php redirect user and use sdk to clear session data.
example redirect url. yourdomain.com/Logout.php?destroy=true
refer to: https://developers.facebook.com/docs/reference/php/facebook-destroySession/
<?php
session_start();
require './src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'appID',
'secret' => 'appSecret',
'cookie' => true, // enable optional cookie support
));
if(isset($_GET['destroy'])){
$facebook->destroySession();
}
?>
It is not recommended to call getAccessToken(); over and over the way you described.
You should try first to see if the token is in the browser as a session and if not then try to use the SDK to find or request it.
Check browser for a user, if no user call sdk for user, if still null or 0 auth or reauth.
If user, check browser for access_token, if no token call sdk for token.
<?php
session_start();
include('config.php');
require('phpsdk.php');
// need to replace Your_App_ID and YourAppId with your applications id.
$facebook = new Facebook(array(
'appId' => 'Your_App_ID',
'secret' => 'Your_App_Secret',
'cookie' => true, // enable optional cookie support
));
if(isset($_SESSION['fb_YourAppId_user_id'])){
$user = $_SESSION['fb_YourAppId_user_id'];
}else{
$user = $facebook->getUser();
}
if($user){
if(isset($_SESSION['fb_YourAppId_access_token'])){
$access_token = $_SESSION['fb_YourAppId_access_token'];
}else{
$access_token = $facebook->getAccessToken();
}
}
echo $user;
echo "<br /><br />";
echo $access_token;
?>

how can we invite friends on fanpage to like page?

i am creating a fanpage and attach in my facebook apps.
i want to invite my friends form my facebook page to like page and get some offer
so i am using code to display status.
<?php
require 'facebook.php';
$app_id = "xxxxxxxxxxxx";
$app_secret = "xxxxxxxxxxxxxxxxxxxxxx";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
$user = $facebook->getUser();
$signed_request = $facebook->getSignedRequest();
$page_id = $signed_request["page"]["id"];
$page_admin = $signed_request["page"]["admin"];
$like_status = $signed_request["page"]["liked"];
$country = $signed_request["user"]["country"];
$locale = $signed_request["user"]["locale"];
?>
here using facebook api to get pageid or user id and other information .
There is no API which allows you to invite a user to like a page, this is only available in Facebook's frontend
I have the same issue but I intend to use (Requests)
https://developers.facebook.com/docs/reference/dialogs/requests/
You can send their friends a request asking them to like your page ! I guess this is more effective and you decide what should be written in the request!

Facebook significant_other_id

I need a little help about a PHP FB app.
Matter is that I wan't to access the significant_other_id as $relationId.
First I get the authorization by user via link:
https://www.facebook.com/dialog/permissions.request?app_id=XXX&display=page&next=XLINK&response_type=code&perms=publish_stream,user_relationships
Then after authorization in php file I try this:
<?php
include('src/facebook.php');
$facebook = new Facebook(array(
'appId' => 'XX',
'secret' => 'XX',
'cookie' => true,
));
$user_profile = $facebook->api('/me','GET');
echo $user_profile['relationship_status']; //Prints ok, requested info
echo $user_profile['significant_other_id']; //Prints nothing
I wasn't able to get that id :S if anyone has experience on this your reply would be great.
Thanks.
According to the documentation, significant_other comes back as an object containing name and id, so try:
echo $user_profile['significant_other']['id'];

Extended Permissions on Facebook Graph API in PHP

I am trying to write an app using the most recent Facebook PHP SDK. However, any extended permissions I request are simply not requested, it always defaults to the basic request, and does not ask for any of the requested extended permissions.
Here is how I am requesting these permissions:
<?php
require_once("facebook.php");
$app_id = "xxx";
$app_secret = "xxx";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
if(is_null($facebook->getUser())){
$params = array(‘scope’ => ‘user_status,publish_stream,user_photos’);
header("Location:{$facebook->getLoginUrl($params)}");
exit;
}
?>
This file is included at the beginning of any page on which my app is embedded. Any ideas as to why I don't get the extended permissions I want?
Make sure you are using quotes '. It seems that you copied this example from a page or something.
if(is_null($facebook->getUser())){
$params = array('req_perms' => 'user_status,publish_stream,user_photos');
header("Location:{$facebook->getLoginUrl($params)}");
exit;
}
UPDATE: use scope instead of req_perms with the new SDK.
You should pass the 'scope' parameter in a comma separated array.