检查有效的头像 (PHP)
我对 PHP 还很陌生,所以如果您有任何想法或建议为我指明正确的方向,我将不胜感激。
尝试创建一个简单的函数来检查用户的电子邮件地址是否转换为有效的 Gravatar 图像,但 gravatar.com 似乎已更改其标题。
使用 get_headers('[电子邮件受保护]')
返回 200 而不是 302。
以下是来自不良头像图像的标头,这些标头似乎都无法提供帮助,因为它们与有效的头像图像相同:
array(13) {
[0]=>
string(15) "HTTP/1.1 200 OK"
[1]=>
string(13) "Server: nginx"
[2]=>
string(35) "Date: Sun, 26 Jul 2009 20:22:07 GMT"
[3]=>
string(24) "Content-Type: image/jpeg"
[4]=>
string(17) "Connection: close"
[5]=>
string(44) "Last-Modified: Sun, 26 Jul 2009 19:47:12 GMT"
[6]=>
string(76) "Content-Disposition: inline; filename="5ed352b75af7175464e354f6651c6e9e.jpg""
[7]=>
string(20) "Content-Length: 3875"
[8]=>
string(32) "X-Varnish: 3883194649 3880834433"
[9]=>
string(16) "Via: 1.1 varnish"
[10]=>
string(38) "Expires: Sun, 26 Jul 2009 20:27:07 GMT"
[11]=>
string(26) "Cache-Control: max-age=300"
[12]=>
string(16) "Source-Age: 1322"
}
ps 我知道 '& ;d'
参数,但它不会满足我的目的。 :)
编辑:
使用'?d'
而不是'&d'
。 必须是 gravatar.com 的内容。
I'm pretty new to PHP, so if you have any thoughts or suggestions to point me in the right direction, I'd be grateful.
Trying to make a simple function to check if a user's email address translates into a valid Gravatar Image, but it seems gravatar.com has changed their headers.
Using get_headers('[email protected]')
returns a 200 instead of 302.
Here are the headers from a bad gravatar image, none of which seem to be able to help because they are identical to a valid gravatar image:
array(13) {
[0]=>
string(15) "HTTP/1.1 200 OK"
[1]=>
string(13) "Server: nginx"
[2]=>
string(35) "Date: Sun, 26 Jul 2009 20:22:07 GMT"
[3]=>
string(24) "Content-Type: image/jpeg"
[4]=>
string(17) "Connection: close"
[5]=>
string(44) "Last-Modified: Sun, 26 Jul 2009 19:47:12 GMT"
[6]=>
string(76) "Content-Disposition: inline; filename="5ed352b75af7175464e354f6651c6e9e.jpg""
[7]=>
string(20) "Content-Length: 3875"
[8]=>
string(32) "X-Varnish: 3883194649 3880834433"
[9]=>
string(16) "Via: 1.1 varnish"
[10]=>
string(38) "Expires: Sun, 26 Jul 2009 20:27:07 GMT"
[11]=>
string(26) "Cache-Control: max-age=300"
[12]=>
string(16) "Source-Age: 1322"
}
p.s. I am aware of the '&d'
parameter, but it will not serve my purpose. :)
EDIT:
Use '?d'
instead of '&d'
. Must be a gravatar.com 'thang.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
Gravatar 在 'd' 参数中添加了一个选项,这意味着 如果您传入
d=404
,如果没有图片,您会得到一个 404 页面(而不是某些302
重定向到默认图片),而不必使用启发式方法。Gravatar have added an option to the 'd' parameter, meaning that if you pass in
d=404
, you get a 404 page (instead of some302
redirect to a default picture) if there's no picture, rather than having to use heuristics.注意:在撰写本文时,这是唯一的选择。 然而,后来添加了
?d=404
,使得尽管您说您了解
d
参数,但您知道它实际上在适用时返回重定向标头吗? 因此,以下 yields 302 因头像不存在而发现:http://www.gravatar。 com/avatar/3b3be63a4c2a439b013787725dfce802?d=http%3A%2F%2Fwww.google.com%2Fimages%2Flogo.gif
在我看来,您所需要做的就是添加
d
参数然后检查 HTTP 结果代码。NOTE: at the time of writing, this was the only option. However, some later time
?d=404
was added, making Andrew's answer much cleaner.Though you said you know about the
d
parameter, do you know it actually returns a redirect header when applicable? So, the following yields 302 Found because the avatar does not exist:http://www.gravatar.com/avatar/3b3be63a4c2a439b013787725dfce802?d=http%3A%2F%2Fwww.google.com%2Fimages%2Flogo.gif
Seems to me that all you need to do is add that
d
parameter and check the HTTP result code then.我建议您尝试 Lucas Araújo 的 php gravatar 类。
这就是你使用它的方式:
I suggest you try the php gravatar class by Lucas Araújo.
and this is how you'd use it:
检查头像时将“default”参数添加到图像 URL,如果找不到图像,这将提供 302 重定向。
如果你愿意的话,空图像可以返回 404 :)
add the "default" parameter to the image url when checking for a gravatar, this will provide a 302 redirect if the image is not found.
the null image could then return a 404 if you want it to :)
扩展 Andrew Aylett 关于 d=404 的答案,实际上可以使用
d=404
(或default=404
)组成 Gravatar 查询,然后查看 headers if key[0]
包含值 404 或 200。最初的问题可以追溯到三年前。 也许那时 Gravatar 的标题内容略有不同。
Extending the answer by Andrew Aylett about d=404, actually it's possible to compose a Gravatar query with
d=404
(ordefault=404
), then look into headers if key[0]
contains a value 404 or 200.Original question dates back three years ago. Maybe at that time Gravatar's headers stuff was slightly different.
文件名( Content-Disposition: inline; filename="5ed352b75af7175464e354f6651c6e9e.jpg" )与“未找到/无效”Gravatar 图像一致吗? 如果是这样,您可以用它来识别无效图像吗?
Is the filename ( Content-Disposition: inline; filename="5ed352b75af7175464e354f6651c6e9e.jpg" ) consistent for "not found/invalid" Gravatar images? If so, you could use that to identify invalid images?
不确定您在获得此信息后想要如何使用它...但是您可以:
将图像加载到网页上,并附加 onload 或 onerror 处理程序...如果 onload 触发,则您有一个匹配,如果onerror 触发它要么不存在(或者加载它时出现问题)
例如
Not sure exactly how you want to use this info once you get it... but could you:
Load the image on a webpage, with an onload or onerror handler attached... if the onload fires, you have a match, if the onerror fires it either doesn't exist (or there are issues loading it)
e.g.
一个非常糟糕的解决方案可能是将
电子邮件
发布到http://en.gravatar .com/accounts/signup 并检查抱歉,该电子邮件地址已被使用!
...编辑
好的,他们使用一些 cookie 来指示是否是否发生错误...;-)
A really unperformant solution could be to post
email
to http://en.gravatar.com/accounts/signup and check forSorry, that email address is already used!
...edit
Okay, they use some cookie to indicate if an error occured or not ... ;-)