How to get facebook username from my friends list.? - facebook

I want to get the userNames of people in my friends list . How can i achieve that ?
I am using
https://graph.facebook.com/me/friends&access_token=...
This is returning me a json response with all the Name and IDnumber ? I want somehow to get the Username ( Not userID ) ?
There is one way that I access each ID and then get the userName but that will take so long considering I have 500 friends. Is there some shorter way / easier way to do that ?

Ask for the username in the fields parameter:
https://graph.facebook.com/me/friends?fields=username&access_token=...

I can't seem to find anything in the API to get a list of user profiles. But one possible solution is to use batch requests. You can combine each individual user profile request into one large batch. It's still cumbersome, but better than making a separate HTTP request for each friend.
http://developers.facebook.com/blog/post/2011/03/17/batch-requests-in-graph-api/
For example:
$batched_request = '[
{"method":"GET","relative_url":"friend_id_1"},'.'{"method":"GET","relative_url":"friend_id_2"}
]';
$post_url = "https://graph.facebook.com/" . "?batch=" . $batched_request
. "&access_token=" . $access_token . "&method=post";

Related

Get the new like growth with Facebook API

I am using the Facebook API to pull data in JSON format.
I need to pull the new likes growth from the pages that im not an admin.
$pageContent1 = file_get_contents('https://graph.facebook.com/v2.5/' . $page . '/?fields=emails,about,website,category,likes.summary(true),location,new_like_count,name,were_here_count,cover,phone,posts&access_token=' . $token . '');
$parsedJson1 = json_decode($pageContent1);
You can only get this if you do one call on day one, and then another calls for every subsequent days if you're not an admin of the page.
See
https://developers.facebook.com/docs/graph-api/reference/insights/#reading
The only publically available insight metrics are page_fans_country and page_storytellers_by_country, but they are both lifetime metrics, meaning that you only get the absolute value.

Facebook page tab app session across subpages PHP SDK 4

See the full original question further down
Using the latest Facebook PHP SDK 4.4.0, in my main app page I can do the following to get a user id etc.
<?php
FacebookSession::setDefaultApplication(APP_ID, SECRET);
$helper = new FacebookRedirectLoginHelper( PAGE_URL );
$pageHelper = new FacebookPageTabHelper();
$session = $pageHelper->getSession();
echo '<p>You are currently viewing page: '. $pageHelper->getPageId() . '</p>';
// get user_id
echo '<p>User Id: ' . $pageHelper->getUserId() . '</p>';
// **depcrecated** get like status - use for likegates
echo '<p>You have '. ( $pageHelper->isLiked() ? 'LIKED' : 'NOT liked' ) . ' this page</p>';
// get admin status
echo '<p>You are '. ( $pageHelper->isAdmin() ? 'an ADMIN' : 'NOT an ADMIN' ) . '</p>';
?>
This does not work on sub pages of my app ... Why is the session (and amongst other things, the signed request) lost? How can I get them back and how can I get methods such as getUserId() from the the FacebookPageTabHelper to continue to work on sub pages?
full original question
I'm fairly new to Facebook app development and I'm having problems with session management and I just can't seem to be able to wrap my head around it. Of course it doesn't help that the official documentation is almost useless.
My problem is that the page session get lost when moving away from the apps main page to a subpage within the Facebook page tab app iframe.
I use the following PHP code to obtain the session and user id on the main (initial) app page and it works great:
<?php
FacebookSession::setDefaultApplication(APP_ID, SECRET);
$helper = new FacebookRedirectLoginHelper( PAGE_URL );
$pageHelper = new FacebookPageTabHelper();
$session = $pageHelper->getSession();
?>
But it doesn't work on sub pages :( when a user clicks on a menu item (or any other link inside the app/iframe), the session goes bye bye. Which is not ideal as I need the user id of the user to track whether or not that user has completed certain actions. Of course I could send the ID along with every request, but there must be a way to have a persisting session, no?
Is there a way to retrieve the session on a sub page in PHP? If so, how? Or do I have to load additional content using javascript? And how would that work, if I can't keep the session between requests and therefore have no way of identifying which user a request came from? How do others handle this?
What I'd like to avoid is to write my own user session management, which would solve the problem but is simply not in the budget and I was hoping I could work with what Facebook already had on offer. Especially since my app doesn't require user information/permissions of any kind.
Thanks a lot in advance for any info on this topic, greatly appreciated, going in circles here.
Edit to clarify: I thought of just saving the Facebook session in a PHP session cookie, but how would I use that to reconnect with Facebook after changing the page?
I finally managed to solve this problem. I'm not sure whether this is considered the right way or can even be a recommended way of doing this, but it works and since time is of the essence, I don't have much of a choice.
If anybody has any further ideas or suggestions, please comment.
Here's how I did it:
// store the signed request
if(isset($_REQUEST['signed_request'])) {
$_SESSION['signed_request'] = $_REQUEST['signed_request'];
} elseif($_SESSION['signed_request']) {
$_REQUEST['signed_request'] = $_GET['signed_request'] = $_POST['signed_request'] = $_SESSION['signed_request'];
}
// assign the stored signed request to REQUEST, GET and POST vars (the unsavory bit, imo)
$_REQUEST['signed_request'] = $_GET['signed_request'] = $_POST['signed_request'] = $_SESSION['signedRequest'];
FacebookSession::setDefaultApplication(APP_ID, APP_SECRET);
$accessToken = APP_ID . '|' . APP_SECRET;
$this->session = new FacebookSession($accessToken);
$pageHelper = new FacebookPageTabHelper();
$isAdmin = ($this->pageHelper->getPageData('admin')) ? $this->pageHelper->getPageData('admin') : 0;
// get pade id
echo '<p>You are currently viewing page: '. $pageHelper->getPageId() . '</p>';
// get user_id
echo '<p>User Id: ' . $pageHelper->getUserId() . '</p>';
// get admin status
echo '<p>You are '. ( $isAdmin ? 'an ADMIN' : 'NOT an ADMIN' ) . '</p>';

Facebook access tokens format

I am manually building a login system to my application using facebook. Following this tutorial I am told to do some get requests to obtain user information. Echoing an returned token I obtain this
(using FILE_GET_CONTENTS() ):
$access_token = "graph.facebook.com/oauth/access_token?client_id=$client_id&redirect_uri=$redirect_uri&client_secret=$client_secret&code=$user_code") :
echoing $access_token:
access_token = CAAKlMMCiHp(...)ZByh3faLzh &expires=5125282
or this for an app_token ("graph.facebook.com/oauth/access_token?client_id=$client_id&client_secret=$client_secret&grant_type=client_credentials") :
echoing $app_token:
access_token = 744(...)90|vZU2(...)e0-s
The last one to obtain the user information that I have to do is this:
$last_one = "graph.facebook.com/debug_token?access_token=$access_token&app_token=$app_token";
Check above that I have to use $access_token and app_token. The problem is that when I echo $access_token what I obtain is literally access_token = xxxx..., including the "access_token" and the "=" sign instead of just the token.
So, when I try to do my last request ($last_one) I have (with variable replacement):
$last_one = "graph.facebook.com/debug_token?access_token=access_token=xxx&app_token=app_token=xxxx";
To avoid this I removed the first "access_token=" and "app_token=" and successfully eliminated the duplicate. Unfortunately I now have access_token=xxx&access_token=xxx because although one is literally for access and another is to the app both are treated as access_token and their echo also explicits "access_token =" as shown above.
My question is if this is supposed to happen and how can I get only the token? I find it kind of ridiculous to have the need of using substr() just to eliminate the first part of the token "access_token =". Am I supposed to reach the token by a getter part of file_get_contents()? And should I include &expires=xxxx of the first token in the last request? Please refer and shortly read the tutorial from "Confirming identities" to understand what is my dilema. I hope I made myself through, thank you very much!

Get the first product id in a magento system via soap api (2)?

Question: Is there a way (api call) to get the first product id in a magento install via the soap api.
I'm attempting to download all the products from a magento system and insert them into a different database (I do the conversion myself so that's not a bother) What is hard to understand though is how do I get a list of the product id's without getting all of them, if all I know is that the site is up.
Here's the info I have.
soap end point
soap username
soap apikey (aka password)
Here's what I don't know.
the id of any of the products
the date any of the products were created on or last edited.
For my initial load, I have to do a where product id in, because I expect 20 to 40k product lists won't come back in one soap call.
So I call
where id in (1 -> 100) Nope
where id in (101-> 200) Nope..
Now as you can imagine that code smells something fierce. It works, but I have to think there is a better way..
To expand my question: Is there a better way?
I can post the XML that I'm sending if that helps. The language I'm using to create the soap(xml) is vim, so I don't have code I can paste.
Try This
$client = new SoapClient('http://localhost/magento8/index.php/api/soap/?wsdl');
$session = $client->login('soap username', 'soap apikey');
$filters=array('entity_id'=>array(array('lt'=>'1','gt'=>'100')));//get fist 100 result
$result = $client->call($session, 'catalog_product.list',array($filters));
var_dump($result);
for more attributes check this
http://www.magentocommerce.com/wiki/1_-_installation_and_configuration/using_collections_in_magento

Dividing Facebook fullname and save as two arrays

I already use a MySQL database and want to combine with Facebook Connect. I did a fb:registration plugin and there I can get only the full name. How can I divide it into first name and surname and save those as two arrays?
You better use Graph API or FQL to get this info for your users if you want to be sure to get the correct first and last name in places...
With Graph API you can request info for multiple users like this:
GET http://graph.facebook.com/?ids={uid_1},{uid_2},{uid_n}&fields=first_name,last_name
There is some limits for number of users you can request info for (I'm not sure on the numbers, but 50 users works ok) and on URL length (which can be worked around issuing POST request and specifying method parameter equal to get to Graph API
Hope this helps :)
$name_arr = explode(' ',$name,2);
$first = $name_arr[0];
$last = isset($name_arr[1])?$name_arr[1]:'';
The first and last name are separated by the first space ' '.
Now excluding middle name :D
$name_arr = explode(' ',$name,3);
$first = $name_arr[0];
$last = isset($name_arr[1])?(isset($name_arr[2])?$name_arr[2]:$name_arr[1]):'';