php - 获取 Facebook 真实头像 URL

标签 php image api facebook url

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

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

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

但是之前链接的真实图片URL是:http://profile.ak.fbcdn.net/hprofile-ak-snc4/hs356.snc4/41721_1489686594_527_q.jpg

如果您在浏览器中键入第一个链接,它会将您重定向到第二个链接。

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

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

谢谢

最佳答案

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

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

关于php - 获取 Facebook 真实头像 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3535222/

相关文章:

javascript - 解析句子并提取转换值

PHP 和 jQuery : Return incremental results as search runs

python - 图像处理概要

html - mxGraph 导出图像损失 css

ruby - 我如何 "Inherit"一个 Ruby 模块来创建另一个模块?

java - Android 中的 StrictMode 类

php - 在文本区域附加新文本

php - 如何使用 mysql 查询发布新闻

javascript - 如何使用 "this"对任何图像应用操作?

api - 我在哪里可以下载所有必要的类来编写 Hadoop MapReduce 作业?