3

I'm trying to rewrite a Facebook app (a PHP script embedding a small multiplayer Flash game) from old FBML to new iFrame type and it kind of works:

<?php

require_once('facebook.php');

define('FB_API_ID', '182820975103876');
define('FB_AUTH_SECRET', 'XXX');

$facebook = new Facebook(array(
            'appId'  => FB_API_ID,
            'secret' => FB_AUTH_SECRET,
            'cookie' => true,
            ));

if (! $facebook->getSession()) {
    printf('<script type="text/javascript">top.location.href="%s";</script>',
        $facebook->getLoginUrl(
                array('canvas'    => 1,
                      'fbconnect' => 0,
                      #'req_perms' => 'user_location',
        )));

} else {
    try {
        $me = $facebook->api('/me');

        $first_name = $me['first_name'];
        $city       = $me['location']['name'];
        $female     = ($me['gender'] != 'male');
        $fields     = $facebook->api('/me', array(
                          'fields' => 'picture',
                          'type'   => 'large'
                      ));
        $avatar     = $fields['picture'];

        # then I print swf tag and pass first_name;city;avatar to it

    } catch (FacebookApiException $e) {
        print('Error: ' . $e);
    }
}

?>

but I think that the call to get the user profile picture causes my script to perform a 2nd CURL fetch, which is probably avoidable? And also I'd like to use the new GRAPH API and not the old REST API - but I'm not sure how to rewrite that call (and I need to get the large and direct user picture).

Alexander Farber
  • 21,519
  • 75
  • 241
  • 416

2 Answers2

11

If you know the user ID, just use:

<img src="http://graph.facebook.com/<UID>/picture?type=large" />

Also note that you can use that URL to retrieve the contents via cURL. If necessary, you can also use cURL to follow the redirects and get the final URL.

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 
    "http://graph.facebook.com/<UID>/picture?type=large");

curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);

$url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);

curl_close($ch);

var_dump($url);
Community
  • 1
  • 1
Jimmy Sawczuk
  • 13,488
  • 7
  • 46
  • 60
  • Thanks, I wonder if your trick will work for my case (I pass urlencoded string of first_name;city;female;avatar as flashvars to an swf file of a Flash game)... – Alexander Farber May 06 '11 at 16:04
  • I'm not familiar with Flash but as long as you can download an image URL you should be able to use that same URL. Alternatively, you could base64-encode it and pass the raw data into the SWF (or something). – Jimmy Sawczuk May 06 '11 at 16:05
  • No sorry, actually it won't work for me, because I pass that avatar URL to other users of my multiplayer game and they won't have the rights to access that URL... So I'm looking for another way to get the direct user picture URL like http://profile.ak.fbcdn.net/hprofile-ak-snc4/hs1325.snc4/161589_100000177688828_2380736_n.jpg - I've updated my question, sorry for not mentioning that. – Alexander Farber May 06 '11 at 16:17
  • I don't see how they don't have permission; if you know the ID, you have access to their photo, it's public. Nonetheless, you can still follow the hops to get the final response URL - [this question](http://stackoverflow.com/questions/1319621/determine-final-destination-of-a-shortened-url-in-php) looks promising for doing that. – Jimmy Sawczuk May 06 '11 at 16:22
  • 1
    No, you can't send the profile.ak.fbcdn.net picture URL, this is a CDN (content delivery network) link. Which means it it being fetched in the nearest server, so it means that facebook users may get a different URL for the same picture. Some links wont work on other countries etc. You can still use "http://graph.facebook.com//picture?type=large" as the direct URL when loading from flash. like mc.loadMovie("http://graph.facebook.com//picture?type=large"); – dragonjet May 07 '11 at 02:42
5

Instead of making two API calls you could use FQL, something like:

$result = $facebook->api(array(
    'method'=>'fql.query',
    'query'=>'SELECT uid,name,first_name,current_location,sex,pic_big FROM user WHERE uid=me()'
));

Sure the fql.query is not a graph method but still it's the way to use FQL.

ifaour
  • 38,035
  • 12
  • 72
  • 79