php 获取 Facebook 真实个人资料图片 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3535222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Facebook real profile image URL
提问by kire
According to Facebook graph API we can request a user profile picture with this (example):
根据 Facebook 图形 API,我们可以使用此(示例)请求用户个人资料图片:
https://graph.facebook.com/1489686594/picture
https://graph.facebook.com/1489686594/picture
We don't need any Token since it's a public information.
我们不需要任何令牌,因为它是公开信息。
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
但是上一个链接的真实图片网址是: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.
如果您在浏览器上输入第一个链接,它会将您重定向到第二个链接。
Is there any way to get the full URL (second link) with PHP, by only knowing the first link?
有没有办法通过只知道第一个链接来使用 PHP 获取完整的 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.
我有一个函数可以从 URL 获取图像以将其存储在数据库中,但它只有在获取完整图像 URL 时才有效。
Thanks
谢谢
回答by The Surrican
kire is right, but a better solution for your use case would be the following:
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!
回答by serg
You can get it with FQL:
您可以使用 FQL 获取它:
select pic_square from user where uid=1489686594
returns:
返回:
[
{
"pic_square": "http://profile.ak.fbcdn.net/hprofile-ak-snc4/hs356.snc4/41721_1489686594_527_q.jpg"
}
]
Also you can just improve your function that gets picture by url. If you use curlit can automatically follow redirect headers.
您也可以改进通过 url 获取图片的功能。如果您使用curl它可以自动跟随重定向标头。
回答by lorey
Please note that the The Surrican's answer (and possibly others) can drastically increase your script's response time (around +500ms for me on my server). This is because the server issues a request to facebook (because of get_headers()
) and the execution time (computation excluded) extends from this:
请注意,The Surrican 的答案(可能还有其他答案)会大大增加脚本的响应时间(在我的服务器上对我来说大约 +500 毫秒)。这是因为服务器向 facebook 发出请求(因为get_headers()
)并且执行时间(计算除外)由此扩展:
- browser -> server
- server -> browser
- 浏览器 -> 服务器
- 服务器 -> 浏览器
to this:
对此:
- browser -> server
- server -> facebook (request: get_headers)
- facebook -> server (response: get_headers)
- server -> browser
- 浏览器 -> 服务器
- 服务器 -> facebook(请求:get_headers)
- facebook -> 服务器(响应:get_headers)
- 服务器 -> 浏览器
This adds the mentioned 500ms delay. You probably should consider to cache the real url on your server or to load the profile picture via JavaScript. At least take a look at your response time before and after ;)
这增加了提到的 500 毫秒延迟。您可能应该考虑在您的服务器上缓存真实 url 或通过 JavaScript 加载个人资料图片。至少看看你之前和之后的响应时间;)
回答by tfont
@The Surrican,
Nice code! Here is a clean cut code function for such process!
@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);
}
This will return the RAW facebook avatar image URL. Feel free to do whatever you want with it then!
这将返回 RAW facebook 头像图像 URL。随意使用它做任何你想做的事!
回答by JordanBelf
You can also add ?redirect=false
to the end of your URL and then parse the JSON response directly.
您还可以添加?redirect=false
到 URL 的末尾,然后直接解析 JSON 响应。
In your example: https://graph.facebook.com/1489686594/picture?redirect=false
在你的例子中: https://graph.facebook.com/1489686594/picture?redirect=false
More information here https://developers.facebook.com/docs/graph-api/reference/user/picture/
这里有更多信息https://developers.facebook.com/docs/graph-api/reference/user/picture/