The Facebook Graph API provides a user's current location in two formats: name and ID. ID can be something like 104022926303756. Name can be something like Palo Alto, California or Beijing, China. From these two fields, how do I extract the country? All American locations are in the form [City], [State] whereas all non-American locations are in the form [City], [Country]. Can I code something less hacky than:
$states = array(
'Alabama'
'Alaska',
'Arizona',
// ...
);
$country = 'USA';
if (!in_array($locationName, $states)) {
preg_match('#, ([a-z]+$)#i', $locationName, $match);
$country = $match[1];
}
How about using this: http://developers.facebook.com/tools/explorer/?method=GET&path=fql%3Fq%3DSELECT%20current_location%20FROM%20user%20WHERE%20uid%3Dme()
?
As you can see, using FQL on user table, the JSON you'll receive is something like:
{
"data": [
{
"current_location": {
"city": "Turin",
"state": "Piemonte",
"country": "Italy",
"zip": "",
"id": 115351801811432,
"name": "Turin, Italy"
}
}
]
}
There you have the field country, much more readable :-)
EDIT: The link is broken because brakets are missing at the end of query, anyway the FQL query is:
SELECT current_location FROM user WHERE uid=me()
"me()" can be any user ID.
If you want more information you could use the latitude and longitude from https://graph.facebook.com/104022926303756 and feed them into a reverse geocoder.
http://maps.googleapis.com/maps/api/geocode/json?latlng=37.4293,-122.138&sensor=false
The Google API gives a lot of information, but it can be quite useful.
The country code may be a better idenfifier than the country name for example.
There's also the possibility to select output language. Adding &lang=sv gives you "Kalifornien" as the name of the state instead of "California".
It comes inside the signed request when they hit your app. It comes even when they still didn't approve your app.
Related
I am trying to use RESTful API to gather the data from a Google spreadsheet spreadsheet.
I read the document at https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/get but I cannot a way that allows me to specify a specific GID (tabs) of the spreadsheet.
After call the API, I was able to get a value from the first tab of the spreadsheet, but I want to change it to the second page (another GID)
Any help would be appreciated. Thanks.
Edit: Added example:
I have a sample spreadsheet here: https://docs.google.com/spreadsheets/d/1da3e6R08S8OoibbOgshyAQy7m9VRGPOrSGzVIvMSXf4/edit#gid=0
When I want to get the value on A1, I can call the RESTful API:
https://content-sheets.googleapis.com/v4/spreadsheets/1da3e6R08S8OoibbOgshyAQy7m9VRGPOrSGzVIvMSXf4/values/A1?key=API_KEY
And I will get:
{
"range": "Sheet1!A1",
"majorDimension": "ROWS",
"values": [
[
"This is on the first tab!"
]
]
}
But as you see in my spreadsheet, I have to tabs (sheet2), how can I get that value of "This is on the second tab!"?
To specify a specific GID (tabs) of the spreadsheet. Enter your 'sheet_name' with single quotes. Don't miss to add double quotes for range. Hope it helps
var query = {
auth: authClient,
spreadsheetId: 'spreadsheetID',
range: "'sheet1'!A1:D3",
};
$data[] = new Google_Service_Sheets_ValueRange([
'range' => 'MGM!A1:Z' . $rowNum,
'values' => $values
]);
MGM - is your tag name
Hi i'm still new in Facebook-ads-api I know this is a noob question,
I want to get the audience_size for the Interest in a specific country.
on the API sample it can only generate the whole world audience_size of an Interest
use FacebookAds\Object\TargetingSearch;
use FacebookAds\Object\Search\TargetingSearchTypes;
$results = TargetingSearch::search(
TargetingSearchTypes::INTEREST,
null,
'soccer'
);
// Sample Response
{
"data":[
{
"name":"Association football (Soccer)",
"id":6003107902433,
"audience_size":593326800,
"path":[
"Sports and outdoors",
"Sports",
"Association football (Soccer)"
],
"description":null
},
...other results...
]
}
In order to get the estimated audience, you can use the Reach Estimate.
You can see the documentation here: https://developers.facebook.com/docs/marketing-api/reference/ad-account/reachestimate/
Just pass the targeting object (which include the interest and the country) and get the result.
I'm using Facebook Graph API to get all the photos of a user graphPath: me/albums and find something weird with Timeline Photos album.
Photos exist in Timeline album on Facebook, but not returned from API call graphPath/{timeline_photos_album_id}/photos, or they are returned with lower number of photos.
What is the problem?
Logs:
Calling for albums graphPath: me/albums:
.....
{
"can_upload" = 0;
count = 3; !!!!!!!!!!!
"created_time" = "2013-06-18T10:43:27+0000";
description = cool;
from = {
id = 100006100533891;
name = "Mike Mike";
};
id = 1387760311437307;
link = "https://www.facebook.com/album.php?
fbid=1387760311437307&id=100006100533891&aid=1073741829";
name = "Timeline Photos";
privacy = everyone;
type = wall;
"updated_time" = "2013-06-18T10:47:53+0000”;
},
.....
Calling for album photos graphPath/{timeline_photos_album_id}/photos:
{
data = (
);
}
Unfortunately "it is not a bug, it is by design".
I tried the following (with "user_photos" permission) and it works:
Fetch the album photo count for my user:
https://graph.facebook.com/me/albums?fields=id,count&access_token={access_token}
The result is
{
"data": [
{
"id": "{album_id}",
"count": 161,
"created_time": "2010-11-05T15:41:42+0000"
},
....
}
Then query for the photos of this album:
https://graph.facebook.com/{album_id}/photos?fields=id&limit=500&access_token={access_token}
The result is
{
"data": [
{
"id": "{photo_id1}",
"created_time": "2013-12-29T09:59:52+0000"
},
{
"id": "{photo_id2}",
"created_time": "2013-12-17T10:40:26+0000"
},
...
}
I count the correct number of 161 ids in the result. So everything seems to work fine.
You can also test this via FQL:
https://graph.facebook.com/fql?q=select+pid+from+photo+where+album_object_id+=+{album_id}+limit+1000&access_token={access_token}
there have been changes in the FB API recently, did you try to add the fields you want to get back in the result from the API? In the past, API always returned "standard fields", newest you always have to provide a list of fields of what you want to have back in the result such as ....fields=name,id...
I think you are missing the user_photos permission in the Access Token. The end point:
/{timeline_photos_album_id}/photos is working perfectly fine for me when I tried it using the Graph API Explorer to retrieve photos from my Timeline photos album. And, I don't think that it is necessary to provide the fields.
I'll suggest you should try the same using the Graph API Explorer. Just press the Get Access Token button on the top right and check the user_photos permission to test your request.
In case you are getting lesser results, you can try your query using pagination options limit and offset. This sometimes returns more results as compare to normal(nothing about this on docs, just my personal experience). Something like:
{timeline_photo_album_id}/photos?limit=500&offset=0
But again, as you pointed out, there are strange things going on with the API. Take a look at this article. This article refers to it as an expected behavior of the API!
There is the question about fetching count of user friends via FaceBook API.
I want to get the number of friends only have USER_ID of the any facebook user. As I know I can get it using FQL like:
SELECT friend_count FROM user WHERE uid = X
But for some users this value is null or there is no value. Please advice is there another way to get this value?
That user might be keeping that information private. In that case, you cannot fetch that information.
You just check the following link. You need to generate token (by clicking on Get Token you will get it) with data you want from facebook API, need to select friends option to get the friend count.
https://developers.facebook.com/tools/explorer/?method=GET&path=me%3Ffields%3Did%2Cname%2Cfriends&version=v2.8
FB.api(
'/me',
'GET',
{"fields":"id,name,friends,link,email,gender,picture,first_name,last_name,cover"},
function(response) {
// Insert your code here
}
);
after executing this you will get this in response as
{
"id": "XXXXXXXXXX",
"name": "Test test",
"friends": {
"data": [
],
"summary": {
"total_count": 14
}
}
}
total_count will be your friend count
I've noticed some difference in Facebook profiles :
Some of them have the following string format at browser nav bar :
http://facebook.com/john.smith
and the others look like this :
http://www.facebook.com/profile.php?id=100455*
Can someone explain why there is a difference ?
And more important is how can I convert those john.smith like names to id numbers ?
These are alias urls that Facebook offers its users (and pages) - its basically a vanity thing, both the id and the alias url will work the same way.
You can translate the alias (or username) by doing a lookup for that user using the Facebook Graph API. Simply make a GET request to https://graph.facebook.com/John for example - this will serve the following response:
{
"id": "779695190",
"name": "John Chan",
"first_name": "John",
"last_name": "Chan",
"link": "http://www.facebook.com/John",
"username": "John",
"gender": "male",
"locale": "en_US",
"updated_time": "2011-12-27T05:01:06+0000",
"type": "user"
}
response.id is what your interested in.
Test it out: http://developers.facebook.com/tools/explorer?method=GET&path=john
You're not really want to converting the ids to names but getting id by username which is possible by issuing request to Graph API:
GET https:/graph.facebook.com/john.smith?fields=id
Here is a JQuery based solution, where callback is an arbitrary function:
$.get("https://graph.facebook.com/" + name + "?fields=id", function(response){
if(response.error){
callback(false)
}else{
callback(response.id);
}
});