Get friendlist for selected location - facebook

HI,
I am using face api to fetch friend list.Please see this code below:
require '../src/facebook.php';
$facebook = new Facebook(array(
'appId' => '170864786279481',
'secret' => 'cd4b835feb73c358eeb4c4df5c293a42',
'cookie' => true, // enable optional cookie support
));
$session = $facebook->getSession();
$friendsLists = $facebook->api('/me/friends/');
This code gives me all friends of user But I need friends for selected location only.For example I want to get only those friends who are living in city "New york" only.Is it possible in this facebook api.Please guide me.
Regards
Deepak

You should use FQL's friend and user tables.
FQL Usage example:
$query = "SELECT ...";
$params = array(
'access_token' => $session['access_token'],
'query' => $query
);
$url = "https://api.facebook.com/method/fql.query?" . http_build_query($params);
$data = simplexml_load_file($url);
// do something with the response, you should catch errors too.

Related

Trying to post to facebook page with app but get "The user hasn't authorized the application to perform this action"

I am trying to write a script that will post an update to a facebook page timeline. I created an app and created a key for the app. I am using the code below:
require_once HOME_PATH . '/include/facebook/facebook.php';
$facebook = new Facebook(array(
'appId' => FACEBOOK_APP_ID,
'secret' => FACEBOOK_APP_SECRET,
'cookie' => true,
'scope' => 'publish_stream',
));
$user_id = $facebook->getUser();
$access_token = $facebook->getAccessToken();
$attachment = array(
'access_token' => $access_token,
'message' => 'this is my message',
'name' => 'name',
'link' => ROOT_URL . $blog_data['url'] . '.htm',
'caption' => $blog_data['title'],
'description' => $blog_data['title'],
);
if ($image = get_first_image($blog_data['body'])) {
$attachment['picture'] = $image;
}
$facebook->api(FACEBOOK_BLOG_PAGE . '/feed', 'post', $attachment);
i think this key is connected to the app and not to my user, so it sounds like i would have to grant the permission within facebook, but i have seen some other posts telling me that i need to do this in code. The examples aren't very clear.
I know similar questions have been asked but i haven't seen any clear answers yet. Can anyone please clarify this?
If you want to get user_id by this piece of code: $user_id = $facebook->getUser();
The first thing you need, is to login an user through a URL returned by $facebook->getLoginUrl(array('scope' => array('perm', 'perm2'))).
Also, notice, where I put permissions, which I require. Not into the Facebook class constructor, but into the method getLoginUrl.
And lastly, for posting into a page, you need publish_actions permission (not publish_stream).

Facebook API Cant upload streaming audio to page wall

I have a music blog and would like to duplicate all my posts to facebook, but I can't get the api to post streaming audio like I can when I post manually. It does actually post, but the audio is stripped out. Here is my code:
<?php
require 'facebook-php-sdk/src/facebook.php';
$APP_ID = 'MYAPPID';
$APP_SECRET = 'MYAPPSECRET';
$PAGE_ID = 'MYPAGEID';
$ACCESS_TOKEN = 'GENERATEDACCESSTOKEN';
$facebook = new Facebook(array(
'appId' => $APP_ID,
'secret' => $APP_SECRET,
'cookie' => true,
));
$attachment = array(
'message' => 'some message',
'attachment' => '{"media": [{"type": "mp3","src": "http://EXAMPLE.COM/music.mp3", "title": "title", "artist": "artist", "album":"album"}]}',
'access_token' => $ACCESS_TOKEN
);
$result = $facebook->api('/'.$PAGE_ID.'/feed', 'post', $attachment);
if($result){
echo "<p>Posted status update</p>";
}
else {
echo "<p>Unable to post update.</p>";
}
?>
Any Idea how I can fix this? Thanks y'all
That's not possible, only whitelisted partners are able to use Open Graph Music tags.
More info here - https://developers.facebook.com/docs/opengraph/music/

login from one domain and after that getting facebook object in another domain using php sdk 3.1.1

I am logining in and making the authentication processes from one domain (localhost)
$config = array(
appId' => $APP_ID,
'secret' => $APP_SECRET
);
$facebook = new Facebook($config);
$currentUserId = $facebook->getUser();
This works fine and i get an Access Token and a user id.
After that. from the browser i am making a post action to a php file on a remote domain
in the remote php i use the same code the same APP_ID and the same APP_SECRET
$config = array(
'appId' => $APP_ID,
'secret' => $APP_SECRET
);
$facebook = new Facebook($config);
$currentUserId = $facebook->getUser();
this does not! work and get $currentUserId = 0
and that is my problem
I can use (but dont want to):
$facebook->setAccessToken($at);
with the acsess token from that i got form the localhost domain it does work and i get the user id
In older sdk 2.0 I have seen this function setBaseDomain
Is there a way i can use it in 3.1.1?
here's what I do - so when you make the request to the other domain pass along the access_token when you make the API call do something like this -
$config = array(
'appId' => $APP_ID,
'secret' => $APP_SECRET
);
$facebook = new Facebook($config);
$args['access_token'] = $_POST['token'] // or $_GET[];
$me = $facebook->api('/me', 'get', $args);
print_r($me);
So what that does then is say to FB I know the request is coming from a different server, but I have a valid token, please accept this sacrifice to mark zuckerberg and let me do things. It will accept and you're free to do as you please :)

How do I check if a given user ID has authenticated my app?

I'm using the Facebook Graph API and want to check if a user has authenticated my Facebook app by user ID. How do I do this?
You use:
SELECT is_app_user FROM user WHERE uid=USER_ID
This should return:
[
{
"is_app_user": true
}
]
If the user has logged in to your application.
Expanding on ifaour's answer, in PHP this query would look something like this:
<?php
$facebook = new Facebook(
'appID' => YOUR_APP_ID,
'secret' => YOUR_APP_SECRET
);
$result = $facebook->api(array(
'method' => 'fql.query',
'query' => "SELECT is_app_user FROM user WHERE uid=$user_id"
));
$is_installed = $result[0]['is_app_user'];
Here you can batch multiple requests together and avoid using FQL.
Assuming you have already logged into facebook and set the access token to the application access token, you can do this:
$batch = array();
foreach($friendArray AS $friend) {
$batch[] = array(
'method' => 'GET',
'relative_url' => '/' . $friend . '?fields=installed'
);
}
FB()->useApplicationAccessToken();
$batchResponse = FB()->facebook()->api('?batch='.json_encode($batch), 'POST');
Then you can process the batch response with code like this:
$installedUsers = array();
$notInstalledUsers = array();
foreach ($batchResponse AS $response) {
$body = json_decode($response['body'], true);
if (!isset($body['id']))
continue;
$id = $body['id'];
if (isset($body['installed']))
$installedUsers[] = $id;
else
$notInstalledUsers[] = $id;
}

how to use fql query

i am new at facebook application development i want to know how fql query works i used it in my application but i want to know where fql query results are displayed or how they can be displayed if you can't explain plz send me the link because the official documentation didn't helped me either.
Connect to FB using the PHP SDK. (available here)
ini_set('display_errors',1);
error_reporting(E_ALL);
require_once 'library/facebook/src/facebook.php';
Check if the user has logged in to FB
$facebook = new Facebook(array(
'appId' => 'YOUR API ID',
'secret' => 'YOUR APP SECRET',
'cookie' => true, // enable optional cookie support
));
FQL
try {
$me = $facebook->api('/me');
$currentPage=1;
$currentPost=1;
$currentDate="";
$list="";
$arrDate=array();
$fql = "YOUR FQL STATEMENT HERE";
$param = array(
'method' => 'fql.query',
'query' => $fql,
'callback' => ''
);
$fqlResult = $facebook->api($param);//Here you have your FQL result set
foreach($fqlResult as $row){
//Do something to the result
}
} catch (FacebookApiException $e) {
error_log($e);
}
You can use the Facebook Graph Explorer
https://developers.facebook.com/tools/explorer
And select under the Access token field, "FQL query".
Now you can try FQL queries and see the result of them.