简体   繁体   English

Facebook GRAPH API + PHP SDK:获取大型用户图片的URL

[英]Facebook GRAPH API + PHP SDK: get the URL of the large user picture

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: 我正在尝试将旧的FBML中的Facebook应用程序(嵌入小型多人Flash游戏的PHP脚本)重写为新的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);
    }
}

?>

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

If you know the user ID, just use: 如果您知道用户ID,只需使用:

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

Also note that you can use that URL to retrieve the contents via cURL. 另请注意,您可以使用该URL通过cURL检索内容。 If necessary, you can also use cURL to follow the redirects and get the final URL . 如有必要,您还可以使用cURL来跟踪重定向并获取最终的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);

Instead of making two API calls you could use FQL, something like: 您可以使用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()'
));

Sure the fql.query is not a graph method but still it's the way to use FQL. 当然fql.query不是图形方法,但仍然是使用FQL的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM