Facebook 图形 API + PHP SDK:获取用户大图的URL

发布于 2024-11-05 05:50:02 字数 1526 浏览 0 评论 0原文

我正在尝试重写 Facebook 应用程序(嵌入 小型多人 Flash 游戏 的 PHP 脚本)从旧的 FBML 到新的 iFrame 类型,它有点有效:

<?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);
    }
}

?>

但我认为获取用户个人资料图片的调用会导致我的脚本执行第二次 CURL 获取,这可能是可以避免的?而且我还想使用新的 GRAPH API 而不是旧的 REST API - 但我不确定如何重写该调用(并且我需要获取 largedirect 用户图片)。

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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泪痕残 2024-11-12 05:50:02

如果您知道用户 ID,只需使用:

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

另请注意,您可以使用该 URL 通过 cURL 检索内容。如果有必要,您还可以 使用 cURL 来跟踪重定向并获取最终网址

$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);

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);
蘑菇王子 2024-11-12 05:50:02

您可以使用 FQL,而不是进行两次 API 调用,例如:

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

当然 fql.query 不是图形方法,但仍然是使用 FQL 的方式。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文