获取 Facebook 真实的个人资料图片 URL [英] Get Facebook real profile image URL

查看:32
本文介绍了获取 Facebook 真实的个人资料图片 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 Facebook 图形 API,我们可以使用此(示例)请求用户个人资料图片:

According to Facebook graph API we can request a user profile picture with this (example):

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

我们不需要任何 Token,因为它是公开信息.

We don't need any Token since it's a public information.

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

But the real image URL of the previous link is: http://profile.ak.fbcdn.net/hprofile-ak-snc4/hs356.snc4/41721_1489686594_527_q.jpg

如果您在浏览器上输入第一个链接,它会将您重定向到第二个链接.

If you type the first link on your browser, it will redirect you to the second link.

只知道第一个链接,有没有办法用 PHP 获取完整的 URL(第二个链接)?

Is there any way to get the full URL (second link) with PHP, by only knowing the first link?

我有一个函数可以从 URL 中获取图像并将其存储在数据库中,但它只有在获取完整图像 URL 时才有效.

I have a function that gets the image from a URL to store it in the database, but it does work only if it get the full image URL.

谢谢

推荐答案

kire 是对的,但对于您的用例更好的解决方案如下:

kire is right, but a better solution for your use case would be the following:

    // 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!

这篇关于获取 Facebook 真实的个人资料图片 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆