Graph API: Can I make multi-calls at a same time? - facebook-graph-api

In fact, I want to get the cover photo for each user's albums.
So, first I get the albums data:
$albums = $GLOBALS["facebook"]->api("/me/albums",'GET');
Then I retrieve the cover_photo id and ask API to get details about this photo (such as the source)
foreach($albums as $album) {
$data = $GLOBALS["facebook"]->api("/". $album["cover_photo"], 'GET');
}
But this make a lot of calls to the API (one per album) and it's seems that she doesn't like.. Because my application return a timeout error.
Is there another way to do what I want?
Thanks.

You can always use the Batch API, or just use FQL. Something like the below would retrieve the src of all the album covers of the current user:
SELECT src
FROM photo
WHERE pid IN (
SELECT cover_pid
FROM album
WHERE owner = me()
)
You can try it here. Obviously you can choose any field to retrieve from the photo table.

Related

How to get Zomato restaurant ID using restaurant link?

I want to get details of a restaurant in Zomato. I have it's link as the input (https://www.zomato.com/mumbai/fantasy-the-cake-shop-kalyan?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1). By browsing the documentation of Zomato APIs, I didn't found a way to get it.
I tried searching for the restaurant using search API but it returns many results.
Any help will be appreciated
It's a two-step process:
Find out restaurant's city id, in your case, Mumbai's city id through the /cities API. It's a simple query search.
Use the city id from the above API call in the /search API, like, https://developers.zomato.com/api/v2.1/search?entity_type=city&entity_id=3&q=fantasy%20the%20cake%20shop%20kalyan
This would give all the basic information about a restaurant.
View the page's source and search for window.RES_ID
I had the same issue as you described. This Zomato's API approach is at least odd. It's almost immposible to GET any information about restaurant if you don't know res_id in advance and that's not possible to parse since Zomato will deny access.
This worked for me:
Obtain user-key from Zomato API Credentials (https://developers.zomato.com/api)
Search restaurant url via API (https://developers.zomato.com/api/v2.1/search?entity_id=84&entity_type=city&q=RESTAURANT_URL&category=7%2C9). The more specific you will be, the better results you'll get (This url is specified by city to Prague (ID = 84) and categories Daily menus (ID = 7) and Lunch (ID = 9). If there is possibility do specify city, category or cuisine, it helps, but should't be necessary. Don't forget to define GET method in headers.
Loop or filter through json results and search for the wanted url. You might need to use method valueOf() to search for the same url. Be careful, you might need to add "?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1" at the end of your wanted url so it has the same format. Check that through Zomato API Documentation page.
for (i in body.restaurants) {
var url_wanted = restaurant_url + '?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1'
var url_in_json = body.restaurants[i].restaurant.url;
if (url_wanted.valueOf() == url_in_json.valueOf()) {
var restaurant_id = body.restaurants[i].restaurant.id;
}
console.log('Voala! We have res_id:' + restaurant_id);
}
There you have it. It could be easier though.
Hope it helps!
once you have the url of the rseraunt's page you can simply look for a javascript object attribute named "window.RES_ID" and further use it in the api call.

Graph API: Posting as contributor to shared albums

Shared albums is a quite new feature of Facebook.
I'm encountering an issue with the Graph API while trying to publish photos to a shared album. The issue is actually related to the ID of the album.
As user A, I create an album getting the ID 1 and I add user B as contributor.
As user B, I can retrieve the album 1 using the Graph API but the "can_upload" flag is set to "false". Which prevents contributor B to upload photos to this album.
When I try to get the album's information as user B using facebook.com, it looks like the album has a different ID than 1, let's call it 2. When I retrieve album information as user B using ID 2 with Graph API, the "can_upload" flag is correctly showing "true".
Apparently the original's album ID is known as a different ID to album contributors. But how can I find out the ID allowing contributors to upload photos to it?
As there seems to be no API call to retrieve the ID (2) of the album to be used while publishing as contributor (user B), this is how one can manage to get it:
As user B (contributor), get the info of the album using its original
ID (1). Make sure to include field "cover_photo" in the request.
Get the info of the cover photo, using its ID retrieved above. Make
sure to include the field "link" in the request. Parse the "link"
field in order to retrieve the value of the parameter "set" which
contains the album ID.
Get the album ID by extracting it from the value above. The format is
something like "a.1234567890" or "a.1234567890.2345678901" where the
contributor's album ID (2) would be 1234567890.

How To Target Photo(s) For a Page Using Graph API?

I need help understanding whether or not this is the intended behavior or if it's something that is not supported. What I want to do is upload a photo to a Facebook page post that is targeted to a specific country.
The steps I take are:
1) Upload the photo to a public album (e.g. Timeline Photos)
2) Retrieve the uploaded photo id from the album
3) Create an attachment to the post with the photo id from step #2
The problem is that I'm now uploading a photo to a public album that is not targeted to a specific country. What I would like to do is to have BOTH the photo(s) and page contents be restricted by targeting. I see a picture parameter in the api, but I believe this is just for setting the page's profile picture.
Any ideas on how this may be accomplished or whether or not this is even possible? Any code examples would also be welcomed.
After playing around with this more I found that I had to set the 'source' parameter to point to a file in my local file system (pointing to a remote file did not work). Not sure if the latter may be accomplished. Also, you have to make a call to setFileUploadSupport with true.
Pseudo code via PHP:
//Create a temporary file somewhere
$contents = ...
$tmpfname = tempnam(sys_get_temp_dir(), $prefix);
$handle = fopen($tmpfname, "w");
fwrite($handle, $contents);
fclose($handle);
//Add to params
$params['source'] = '#' . $tmpfname
$fb = FB_sdk::instance(true);
$fb->setFileUploadSupport(true);
$fb->api('[url to page]/photos', 'POST', $params);

Artist info missing from Facebook Graph API /music.listens endpoint

I'm playing around w/ the /me/music.listens endpoint of Graph API and I have it working just fine. Except I can't seem to figure out how to get actual artist info to come back. I see the song and even the album (though that seems a little inconsistent too). But never any artist info.
Check the developer explorer here: https://developers.facebook.com/tools/explorer/?method=GET&path=me%2Fmusic.listens
No artist info. Is this just not returned? I can't see how to specify this in a fields param list. FB's documentation of the actions is spotty at best so I figured I'd try here.
Thanks!
I've figured out a work around. It doesn't look like Facebook actually returns artist info. So you have to use the Spotify Lookup Service (http://developer.spotify.com/technologies/web-api/lookup/).
To break it down a little further, you start by pulling the music.listens feed from FB and you'll get info that looks like:
(
[song] => Array
(
[id] => 381659191902248
[url] => http://open.spotify.com/track/**2Vv27Gc5k5GgGW9jgtj1CS**
[type] => music.song
[title] => Follow Through
)
)
From there, you need to grab the bolded track ID.
Lastly, fetch the song metadata with this call to Spotify: http://ws.spotify.com/lookup/1/.json?uri=spotify:track:2Vv27Gc5k5GgGW9jgtj1CS
You'll be returned the album name, artist info, etc.
If someone knows a better way, lemme know!
You can retrieve artists informations on graph API with the song id :
https://graph.facebook.com/song_id?fields=data{musician}
ex : https://graph.facebook.com/697975930266172?fields=data{musician}
Old question I know, but you can retrieve related information such as both the song title and artist title in the same request using Graph API's 'field expansion'.
For example - Last 10 songs
me?fields=music.listens.limit(10){data{song{title,id},musician{id,title}}}
To retrieve more information from either the song or musician entities just tweak the fields requested.
Note, this requires user_actions.music permission

Display name & picture knowing ID

I was looking to find an answer to my question, but so far I got this:
https://graph.facebook.com/the_user_id?fields=name,picture
I need to be able to display/print first,last name and picture of a set list of users for which I know their ID. What code is required to get this data and then to publish it on a php/html page? Of course, this will means that if I want to show 10 users, I will input 10 different IDs (read smtg about an array list?). Notice that I DO NOT require for this to work for the current user.
Thanks in advance for your replies.
You need to use file_get_contents ( http://uk3.php.net/file_get_contents ) or curl in php and issue a request to the url such as follows:
https://graph.facebook.com/?ids=id1,id2,id3&fields=name,picture
(replacing id1,id2 with your ids)
this will then return you a json object. You then need to decode ( http://uk3.php.net/json_decode ) and loop through this and access the information
this should get you started
// people array uses the users id as the key and the dessert as the value. The id is then used in the query to facebook to select the corresponding value from this array
$people = array("id1"=>"favourite "dessert", "id2"=>"favourite dessert", "id3"=>"apple pie");
$json = file_get_contents('https://graph.facebook.com/?ids=id1,id2,id3&fields=id,name,picture');
$json = json_decode($json);
foreach($json as $key=>$person){
echo '<p><img src="'.$person->picture.'" alt="'.$person->name.'" />';
echo $person->name.'\'s favourite dessert is '.$people[$person->id'];
echo '</p>';
}
I've batched the requests here, alternatively you could perform 10 separate queries for each user, but that would be a bit pointless and inefficient
The easiest way is with an FQL query:
SELECT first_name, last_name, pic, uid FROM user WHERE uid IN
(Known_ID_1, Known_ID_2, ... Known_ID_n)
The easiest, if you're using PHP is to install the PHP SDK, though you can also make a call directly to https://graph.facebook.com/fql?q=URL_ENCODED_QUERY