【问题标题】:Get Facebook real profile image URL获取 Facebook 真实头像 URL
【发布时间】:2011-04-01 21:40:40
【问题描述】:

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

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

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

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

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

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

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

谢谢

【问题讨论】:

  • 非常感谢 Ben,它运行良好。我正在使用它,以便当用户在我的网站上连接 Facebook 时,我可以抓取他们的 Facebook 个人资料图片,将其设为默认图片。谢谢:)
  • @kire,FaceBook 开发者 API 不是为您提供了官方解决方案吗?我以前从未使用过它,但我认为他们会这样做。通常在未经同意的情况下接管其他网站资源可能会导致 IP 禁令。
  • @Ben “未经同意接管其他网站资源”是什么意思?你的意思是使用或改编某人共享的功能来帮助他人是不好的?不要认为共享服务器端功能的人期望人们征求他的同意才能理解并可能使用他的功能......
  • @kire,我不是说我给你的功能,我是指 Facebook 的资源。如果图像的来源不知道发生了什么,热链接图像通常被视为一种不好的做法和/或粗鲁。阅读 Facebook TOS 和 Facebook Developers TOS,以确保您可以在不使用 API 的情况下执行此操作。

标签: php image api facebook url


【解决方案1】:

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!

【讨论】:

【解决方案2】:

您可以通过 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,它可以自动跟随重定向头。

【讨论】:

  • 我尝试了类似的解决方案,但 Facebook“pic_square”属性返回 50px*50px,在我的情况下,我需要稍微大一点才能不松散质量。我只能通过请求此表单来使用图形 API:graph.facebook.com/xxxxxxxxx/picture?type=large 但是非常感谢现在有了一个很好的解决方案。
  • @kire 有 4 种不同的图片可用,选择你需要的:developers.facebook.com/docs/reference/fql/user
  • 哦,是的,确实没有找到那个页面!现在我的问题是知道是请求 Graph API URL 并获取真实图片 URL 还是使用 FQL 更好。也许第一个解决方案对我来说会更简单,因为我可以将它添加到我的 Facebook 类并在需要时调用它,而无需任何 Javascript 左右。你们真的很擅长你的工作:)
  • 抱歉,我可以通过查询该页面得到相同的结果:api.facebook.com/method/fql.query?query=SELECT pic_big FROM user WHERE uid = 1489686594 嗯看起来更简单...
  • @kire 我喜欢有人在 facebook 上实际使用“xxxxxxxxx”作为用户名,并且您的示例链接解析为他的个人资料图片。
【解决方案3】:

请注意,The Surrican 的回答(可能还有其他人)会大大增加脚本的响应时间(我的服务器上大约 +500 毫秒)。这是因为服务器向 facebook 发出请求(因为get_headers()),并且执行时间(不包括计算)由此延伸:

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

到这里:

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

这增加了提到的 500 毫秒延迟。您可能应该考虑在服务器上缓存真实的 url 或通过 JavaScript 加载个人资料图片。至少看看你之前和之后的响应时间;)

【讨论】:

【解决方案4】:

@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 facebook 头像图像 URL。那就随心所欲地做任何你想做的事吧!

【讨论】:

    【解决方案5】:

    您也可以在 URL 的末尾添加?redirect=false,然后直接解析 JSON 响应。

    在您的示例中:https://graph.facebook.com/1489686594/picture?redirect=false

    更多信息在这里https://developers.facebook.com/docs/graph-api/reference/user/picture/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多