2010-08-20 115 views
11

根据Facebook的图形API,我们可以要求用户的个人资料图片与此(例如):获取Facebook的真实个人资料图片URL

https://graph.facebook.com/1489686594/picture

我们不需要任何令牌,因为它是一个公共信息。

但一个链接的真实图像网址是:http://profile.ak.fbcdn.net/hprofile-ak-snc4/hs356.snc4/41721_1489686594_527_q.jpg

如果键入浏览器上的第一个链接,它会重定向到第二个环节。

有什么办法来获取完整URL(第二个链接)与PHP,只知道第一个链接?

我有一个从URL中获取的图像,将其存储在数据库中的功能,但如果它得到完整的图像URL它不只是工作。

感谢

+0

是的,你可以做到这一点http://w-shadow.com/blog/2008/07/05/how-to-get-重定向url-in-php/ – Ben 2010-08-20 22:14:56

+0

哦,非常感谢Ben,它完美的工作。我正在使用它,以便当用户在我的网站上与Facebook连接时,我可以获取他们的Facebook个人资料图片,使其成为他们的默认图片。 谢谢:) – kire 2010-08-20 22:27:17

+0

@kire,不FaceBook开发人员API给你一个正式的解决方案来做到这一点?我从来没有用过它,但我认为他们这样做。通常在没有协议的情况下接管其他网站资源可能会导致知识产权禁令。 – Ben 2010-08-20 22:33:09

回答

19

基雷是正确的,但对你的使用情况更好的解决方案将是如下:

// get the headers from the source without downloading anything 
    // there will be a location header wich redirects to the actual url 
    // you may want to put some error handling here in case the connection cant be established etc... 
    // the second parameter gives us an assoziative array and not jut a sequential list so we can right away extract the location header 
    $headers = get_headers('https://graph.facebook.com/1489686594/picture',1); 
    // just a precaution, check whether the header isset... 
    if(isset($headers['Location'])) { 
     $url = $headers['Location']; // string 
    } else { 
     $url = false; // nothing there? .. weird, but okay! 
    } 
    // $url contains now the url of the profile picture, but be careful it might very well be only temporary! there's a reason why facebok does it this way ;) 
    // the code is untested! 
+1

经过测试!它效果很好 – Rafalages 2010-11-18 03:02:08

+0

请考虑通过[Facebook Graph API]添加有关通过JSON获取位置的信息(https://developers.facebook.com/docs/reference/api/using-pictures/#json) – Samveen 2013-07-25 09:18:08

+0

今天工作。 – 2014-04-16 16:39:08

7

你可以用FQL得到它:

select pic_square from user where uid=1489686594 

回报:

[ 
    { 
    "pic_square": "http://profile.ak.fbcdn.net/hprofile-ak-snc4/hs356.snc4/41721_1489686594_527_q.jpg" 
    } 
] 

而且你可以提高你的函数,通过URL获取图片。如果您使用curl,它可以自动遵循重定向标题。

+0

我试过类似的解决方案,但Facebook的“pic_square”propertie返回一个50px * 50px,在我的情况下,我需要一个更大更轻松不松动质量。 我只能通过图表API请求此表单:https://graph.facebook.com/xxxxxxxxx/picture?type=large 但是,非常感谢现在有了一个很好的解决方案。 – kire 2010-08-20 22:29:47

+3

@ kire有4个不同的图片可供选择,请选择你需要的:http://developers.facebook.com/docs/reference/fql/user – serg 2010-08-20 22:31:49

+0

哦,是的,真的没有找到该页面! 现在对我来说,问题是要知道是否最好只要求Graph API URL并获取真实图片URL或使用FQL。 也许第一个解决方案对我来说会更简单,因为我可以将它添加到我的Facebook类中,并随时调用它而不需要任何Javascript或以上。你们真的很擅长你:) – kire 2010-08-20 22:39:26

2

请注意,该Surrican的答案(可能还有其他人)可以显着提高你的脚本的响应时间(约+在我的服务器上为我500ms)。这是因为服务器发出到(因为get_headers())Facebook和执行时间(计算除外)的请求,从该延伸:

  • 浏览器 - >服务器
  • 服务器 - >浏览器

此:

  • 浏览器 - >服务器
  • 服务器 - >的Facebook(请求:get_headers)
  • 的Facebook - >服务器(响应:get_headers)
  • 服务器 - >浏览器

这增加了所提到的500ms的延迟。您可能应该考虑在您的服务器上缓存真实的URL或通过JavaScript加载配置文件图片。至少看看你前后的反应时间;)

+1

关于响应时间的真棒观察。结合[图形API的JSON结果](https://developers.facebook.com/docs/reference/api/using-pictures/#json),javascript肯定会是最好的答案。 – Samveen 2013-07-25 09:20:39

2

@The Surrican,
不错的代码!这是一个干净的代码功能,用于这样的过程!

function get_raw_facebook_avatar_url($uid) 
{ 
    $array = get_headers('https://graph.facebook.com/'.$uid.'/picture?type=large', 1); 
    return (isset($array['Location']) ? $array['Location'] : FALSE); 
} 

这将返回RAW脸像图像的URL。随时随地做任何你想做的事!

相关问题