Reputation: 6108
Upon user registration I fetch the users data (name, birthday, location, etc) and also the users profile image.
Currently, I would be able to do this: /me?fields=picture,name
, but the picture returned is too low resolution, so I'd like to fetch the picture with the height parameter (as described here).
My only option, as I see it, is to part it up in two requests, which seems inefficient:
me/?fields=name
me/picture?height=500&redirect=false
Would there be a way to merge this into ONE request?
Upvotes: 2
Views: 3484
Reputation: 73984
Use field expansion, just one API call:
/me?fields=name,picture.width(500).height(500),birthday
API Explorer test: https://developers.facebook.com/tools/explorer/?method=GET&path=me%3Ffields%3Dname%2Cpicture.width%28500%29.height%28500%29&version=v2.2
More information about field expansion: https://developers.facebook.com/docs/graph-api/using-graph-api/v2.2#fieldexpansion
Batch Requests would be another solution, although not a very good one in that case because they are a lot more complicated (more code, more things to consider) and they don´t count as one call:
each call within the batch is counted separately for the purposes of calculating API call limits and resource limits
...meaning, they are only faster than 2 separate calls (as fast as the slowest call in the batch), but field expansion is really just 1 call. Batch Requests are for "potentially unrelated Graph API calls".
Upvotes: 9
Reputation: 9227
You can use batch
as per the docs here
Here's an example request for your needs:
URL: https://graph.facebook.com/v2.2/
POST data (important! not GET!):
batch=[{"method":"GET", "relative_url":"me/?fields=name"},{"method":"GET", "relative_url":"me/picture?height=500&redirect=false"}]
Optional but recommended: include_headers=false
Example response:
[
{
"code": 200,
"body": "{\n \"name\": \"Richard Down\",\n \"id\": \"704787476294950\"\n}"
},
{
"code": 200,
"body": "{\n \"data\": {\n \"height\": 604,\n \"is_silhouette\": true,\n \"url\": \"https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xap1/t31.0-1/s960x960/10506738_10150004552801856_220367501106153455_o.jpg\",\n \"width\": 960\n }\n}"
}
]
EDIT: As you've tagged this with facebook-php-sdk, here's an example using that:
$requests = array(
array('method' => 'GET', 'relative_url' => 'me/?fields=name'),
array('method' => 'GET', 'relative_url' => 'me/picture?height=500&redirect=false')
);
$response = json_decode($facebook->api('?batch=' . json_encode($requests), 'POST'), true);
$name = $response[0]['name'];
$image = $response[1]['url'];
Upvotes: 1