Dividing Facebook fullname and save as two arrays - facebook

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]):'';

Related

Get follower count on Scratch (API)

I am looking to find the follower count of a Scratch user using the Scratch API. I already know how to get their message count, with https://api.scratch.mit.edu/users/[USER]/messages/count/.
This answer targets the Scratch REST API, documented here.
You get the user's followers by requesting them: https://api.scratch.mit.edu/users/some_username/following where some_username is to be replaced by the actual username.
This will return 0 to 20 results (20 is the default limit of objects returned by the REST API). If there's less than 20 results, then you're done. The amount of followers is simply the count of the objects returned.
If there's 20 objects returned, we can't be certain we've requested all the user's friends as there might be more to come. Therefore, we skip the first 20 followers of that user by supplying the ?offset= parameter: https://api.scratch.mit.edu/users/some_username/following?offset=20
This retrieves the second 'page' of friends. Now we simply loop through the procedure described above, incrementing offset by 20 each time until either less than 20 results are returned or no results are returned. The amount of friends of that user is the cumulative count of the objects returned.
As mentioned by _nix on this forum thread, there is currently no API to achieve this. However, he/she rightly points out that the number can be obtained from a user's profile page.
You may write a script (in JavaScript, for example) to parse the HTML and get the follower count in the brackets at the top of the page.
Hope this helps!
There is a solution in Python:
import requests
import re
def followers(self,user):
followers = int(re.search(r'Followers \(([0-9]+)\)', requests.get(f'https://scratch.mit.edu/users/{user}/followers').text, re.I).group(1))
return f'{followers} on [scratch](https://scratch.mit.edu/users/{user}/followers)'
Credit goes to 12944qwerty, in his code (adapted to remove some implementation specific stuff).
use ScratchDB
var user = "username here";
fetch(`https://scratchdb.lefty.one/v3/user/info/${user}`).then(res => res.json()).then(data => {
console.log(`${user} has ` + data["followers"].toString() + " followers");
}
(Edit: this is javascript btw, I prefer Python but Python doesn't have a cloud.set function and this is how I did it)
Use ScratchDB (I used httpx, but you can GET with anything):
import httpx
import json
user = "griffpatch"
response = httpx.get(f"https://scratchdb.lefty.one/v3/user/info/{ user }")
userData = json.loads(response.text)
followers = userData["statistics"]["followers"]
https://api.scratch.mit.edu/users/griffpatch/followers
this gives the follower names, scratch staus(scratch team or not), pfp, everything in their profile

I can not get the number of followers on a page

I'm trying to get the number of followers on a page. That is, obtain the number that appears in the portal as: "1363 people follow this"
I am trying to make the call as follows:
Dim urlSocialAnalitics As String =
String.Format ("https://graph.facebook.com/{0}/friends?summary=total_count&access_token={1}", pIdFanPage, pToken)
But it does not work. It's a problem for me.
I have also tried with this url:
urlSocialAnalitics = String.Format("graph.facebook.com/v2.6/{0}?fields=fan_count&access_token={1}", pIdFanPage, pToken)
But this gives me the number of Likes and not of Followers
I have read the documentation:
https://developers.facebook.com/docs/graph-api/reference/page/
But I do not see anything about it.
What is the correct call?

Facebook::Graph for number of likes

Can I use Facebook::Graph to retrieve the number of likes without dealing with authorization tokens? The following code:
use Facebook::Graph;
my $fb = Facebook::Graph->new;
my $hashref = $fb->query
->request('https://graph.facebook.com/btaylor')
->as_hashref;
produces the following error:
Unable to create sub named ""
I'm not a real perl coder so I may be way off.
http://search.cpan.org/~rizen/Facebook-Graph-1.0501/lib/Facebook/Graph.pm
You can. Your problem is that btaylor is a regular facebook user and users cannot be liked. For objects that can be liked you can query for a list of likes following the example below.
https://graph.facebook.com/thagroggs

How to get facebook username from my friends list.?

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";

I am having problems running Facebook FQL queries that include long user ids

I am having problems running queries with FQL that include a supplied "Large"(beginning with 10000..) User ID
here is an example of one that is not working:
fql?q=SELECT uid, first_name,last_name,pic,pic_square,name
FROM user
WHERE uid=100002445083370
Is there a way to encapsulate the long number so it's passed as a string?
here is another example:
/fql?q=SELECT src_big
FROM photo
WHERE aid IN (SELECT aid
FROM album
WHERE owner=100002445083370 AND type="profile")
ORDER BY created DESC LIMIT 1
Has anyone been able to solve this issue? I am testing the queries in the graph explorer with no luck as well.
I see what the problem is,
The User id I am trying to pass is supposed to be: "100002445083367", but from querying the list of friends and grabbing their User Id, I am getting back "uid":1.0000244508337e+14 which is being shortened to: 100002445083370 (php removing the e+14) throwing off the second query. I need to make sure the id I am grabbing is staying as a string value not a number while I pass it back and forth from PHP and Javascript.
The problem is because of the way PHP handles JSON_DECODE. I had to modify Facebook PHP SDK and add a preg_replace previous to the json_decode. It will make sure json_decode doesn't convert large integers to floats by first converting them to strings.
here is the code:
line 803 from base_facebook.php:
$result = json_decode(preg_replace('/("\w+"):(\d+)/', '\\1:"\\2"', $this->_oauthRequest($this->getUrl($domainKey, $path),$params)), true);
here is more information on the subject:
http://forum.developers.facebook.net/viewtopic.php?id=20846
What do you mean by "not working"?
That query works for me in Graph API explorer but the response is
{
"data": [
]
}
I think that user-id isn't valid; https://www.facebook.com/profile.php?id=100002445083370 gives a "Page not found" error for me.