检查有效的gravatar(PHP)
我对PHP非常陌生,所以如果您有任何想法或建议可以为我指明正确的方向,我将不胜感激 尝试制作一个简单的函数来检查用户的电子邮件地址是否转换为有效的Gravatar图像,但Gravatar.com似乎已经更改了它们的标题 使用检查有效的gravatar(PHP),php,gravatar,Php,Gravatar,我对PHP非常陌生,所以如果您有任何想法或建议可以为我指明正确的方向,我将不胜感激 尝试制作一个简单的函数来检查用户的电子邮件地址是否转换为有效的Gravatar图像,但Gravatar.com似乎已经更改了它们的标题 使用get_头('urlencoded_bad_email@example.com)返回200而不是302 以下是不良gravatar图像的标题,这些标题似乎都没有帮助,因为它们与有效的gravatar图像相同: array(13) { [0]=> string(1
get_头('urlencoded_bad_email@example.com)
返回200而不是302
以下是不良gravatar图像的标题,这些标题似乎都没有帮助,因为它们与有效的gravatar图像相同:
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、 我知道”&;d'
参数,但它不适用于我的目的。:)
编辑:
使用“?d”
代替”&;d'
。一定是gravatar.com'thang.我建议你试试Lucas Araújo的
/**
* Class Gravatar
*
* From Gravatar Help:
* "A gravatar is a dynamic image resource that is requested from our server. The request
* URL is presented here, broken into its segments."
* Source:
* http://site.gravatar.com/site/implement
*
* Usage:
* <code>
* $email = "youremail@yourhost.com";
* $default = "http://www.yourhost.com/default_image.jpg"; // Optional
* $gravatar = new Gravatar($email, $default);
* $gravatar->size = 80;
* $gravatar->rating = "G";
* $gravatar->border = "FF0000";
*
* echo $gravatar; // Or echo $gravatar->toHTML();
* </code>
*
* Class Page: http://www.phpclasses.org/browse/package/4227.html
*
* @author Lucas Araújo <araujo.lucas@gmail.com>
* @version 1.0
* @package Gravatar
*/
class Gravatar
{
/**
* Gravatar's url
*/
const GRAVATAR_URL = "http://www.gravatar.com/avatar.php";
/**
* Ratings available
*/
private $GRAVATAR_RATING = array("G", "PG", "R", "X");
/**
* Query string. key/value
*/
protected $properties = array(
"gravatar_id" => NULL,
"default" => NULL,
"size" => 80, // The default value
"rating" => NULL,
"border" => NULL,
);
/**
* E-mail. This will be converted to md5($email)
*/
protected $email = "";
/**
* Extra attributes to the IMG tag like ALT, CLASS, STYLE...
*/
protected $extra = "";
/**
*
*/
public function __construct($email=NULL, $default=NULL) {
$this->setEmail($email);
$this->setDefault($default);
}
/**
*
*/
public function setEmail($email) {
if ($this->isValidEmail($email)) {
$this->email = $email;
$this->properties['gravatar_id'] = md5(strtolower($this->email));
return true;
}
return false;
}
/**
*
*/
public function setDefault($default) {
$this->properties['default'] = $default;
}
/**
*
*/
public function setRating($rating) {
if (in_array($rating, $this->GRAVATAR_RATING)) {
$this->properties['rating'] = $rating;
return true;
}
return false;
}
/**
*
*/
public function setSize($size) {
$size = (int) $size;
if ($size <= 0)
$size = NULL; // Use the default size
$this->properties['size'] = $size;
}
/**
*
*/
public function setExtra($extra) {
$this->extra = $extra;
}
/**
*
*/
public function isValidEmail($email) {
// Source: http://www.zend.com/zend/spotlight/ev12apr.php
return eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email);
}
/**
* Object property overloading
*/
public function __get($var) { return @$this->properties[$var]; }
/**
* Object property overloading
*/
public function __set($var, $value) {
switch($var) {
case "email": return $this->setEmail($value);
case "rating": return $this->setRating($value);
case "default": return $this->setDefault($value);
case "size": return $this->setSize($value);
// Cannot set gravatar_id
case "gravatar_id": return;
}
return @$this->properties[$var] = $value;
}
/**
* Object property overloading
*/
public function __isset($var) { return isset($this->properties[$var]); }
/**
* Object property overloading
*/
public function __unset($var) { return @$this->properties[$var] == NULL; }
/**
* Get source
*/
public function getSrc() {
$url = self::GRAVATAR_URL ."?";
$first = true;
foreach($this->properties as $key => $value) {
if (isset($value)) {
if (!$first)
$url .= "&";
$url .= $key."=".urlencode($value);
$first = false;
}
}
return $url;
}
/**
* toHTML
*/
public function toHTML() {
return '<img src="'. $this->getSrc() .'"'
.(!isset($this->size) ? "" : ' width="'.$this->size.'" height="'.$this->size.'"')
.$this->extra
.' />';
}
/**
* toString
*/
public function __toString() { return $this->toHTML(); }
}
下面是您使用它的方式:
include 'gravatar.php';
$eMail = 'name@email.net';
$defImg = 'http://www.example.com/images/myphoto.jpg';
$avatar = new Gravatar($eMail, $defImg);
$avatar->setSize(90);
$avatar->setRating('G');
$avatar->setExtra('alt="my gravatar"');
<p>
<?php echo $avatar->toHTML(); ?>
</p>
包括“gravatar.php”;
$eMail='1name@email.net';
$defing=http://www.example.com/images/myphoto.jpg';
$avatar=newgravatar($eMail,$defing);
$avatar->setSize(90);
$avatar->setRating('G');
$avatar->setExtra('alt=“my gravatar”);
文件名(内容处置:inline;filename=“5ed352b75af7175464e354f6651c6e9e.jpg”)是否与“未找到/无效”Gravatar图像一致?如果是这样,您可以用它来识别无效图像?一个真正不好的解决方案可能是将电子邮件发送到并检查对不起,该电子邮件地址已被使用代码>
编辑
好的,他们使用一些cookie来指示是否发生了错误…;-)
不知道你想要如何使用这些信息,一旦你得到它。。。但你能:
将图像加载到网页上,并附加onload或onerror处理程序。。。如果onload触发,则表示您有一个匹配项;如果onerror触发,则表示该匹配项不存在(或者加载时出现问题)
e、 g
注意:在撰写本文时,这是唯一的选择。然而,后来又增加了?d=404
,使其更加干净
虽然您说过您知道,但您知道它实际上会在适用时返回重定向头吗?因此,发现以下302是因为化身不存在:
在我看来,您所需要做的就是添加d
参数,然后检查HTTP结果代码。在检查gravatar时,将“默认”参数添加到图像url,如果找不到图像,这将提供302重定向
$grav_url = 'http://www.gravatar.com/avatar/'.md5(mb_strtolower($email)).'?default=http://www.mysite.com/null.jpg&size=310';
如果您希望,空图像可以返回404:)Gravatar在“d”参数中添加了一个选项,这意味着,如果没有图片,您将得到一个404页面(而不是一些302
重定向到默认图片),而不必使用启发式,实际上,可以使用d=404
(或default=404
)组合Gravatar查询,然后如果键[0]
包含值404或200,则可以查看标题
$email = md5(strtolower("myemailaddress@example.com"));
$gravatar = "http://www.gravatar.com/avatar/$email?d=404";
$headers = get_headers($gravatar,1);
if (strpos($headers[0],'200')) echo "<img src='$gravatar'>"; // OK
else if (strpos($headers[0],'404')) echo "No Gravatar"; // Not Found
$email=md5(strtolower(“myemailaddress@example.com"));
$gravatar=”http://www.gravatar.com/avatar/$email?d=404”;
$headers=get_headers($gravatar,1);
if(strpos($headers[0],'200'))回显“”;//好啊
else if(strpos($headers[0],'404'))回显“No Gravatar”;//找不到
最初的问题可以追溯到三年前。可能当时Gravatar的Header内容略有不同。我建议修改该类以替换对eregi的已弃用(自5.3版起)调用,并使用preg_match with/Ith回复非常感谢,但我只需要一个函数来检查有效的Gravatar映像。如果未找到有效图像,则函数需要返回FALSE。除非我忽略了它,否则我在上面的类中没有看到这方面的代码。(只是一个旁注:根据RFC2607,在文档中请始终使用@example.com、@example.org或@example.net——不需要让address.com上的人收到你的垃圾邮件。)啊!我也知道。哈哈,修正了。我希望它是一致的,但对于每个电子邮件地址来说都是不同的,不管它是否是有效的gravatar:(即使文件名不是,无效电子邮件地址的内容是否相同?也许您可以使用已知“无效”的MD5哈希值)响应并使用它进行比较…是的,无效电子邮件地址的内容是相同的,即使文件名不是。问题是,有效响应与无效响应是相同的。我不认为Gravatar的员工会对这种方法过于热情。;-)那么他们应该提供一个API…;-)re:空气污染指数;;我完全同意。他们最近把一个无效的雕刻机上的302改成了200。。。可能就是为了这个目的。为什么他们不允许网站管理员检查无效的图像?没有意义。我不认为这会起作用,因为图像从未失败过。。。回答总是200。不确定为什么要修改,我在生产环境中工作得很好。我也不确定,可能是因为原始问题已经提到了默认参数。为了因果报应和帮助,我投了赞成票。或者,正如我一天前发布的答案一样?无论如何,这已经被安德鲁的回答淘汰了,所以我想我的和你的都可以被删除了!这很有效。似乎决定因素是使用“?d=”而不是使用“&;默认gravatar的d='。第一个GET参数的前缀必须始终带有问号;所有后续参数都使用符号AND分隔。将302状态代码解释为故障是非常可疑的。只需使用404默认值,就可以设置了。@cweiske,如果Gravatar返回404s,则为真。但是那么,一个人怎么能对一个没有返回的404进行操作呢?啊,我明白了,?d=
,因为一段时间也允许简单的?d=404
@杰夫,你能接受安德鲁的回答吗,这样我就可以删除这个了?
HTTP/1.1 302 Found
...
Last-Modified: Wed, 11 Jan 1984 08:00:00 GMT
Location: http://www.google.com/images/logo.gif
Content-Length: 0
...
Expires: Sun, 26 Jul 2009 23:18:33 GMT
Cache-Control: max-age=300
$grav_url = 'http://www.gravatar.com/avatar/'.md5(mb_strtolower($email)).'?default=http://www.mysite.com/null.jpg&size=310';
$email = md5(strtolower("myemailaddress@example.com"));
$gravatar = "http://www.gravatar.com/avatar/$email?d=404";
$headers = get_headers($gravatar,1);
if (strpos($headers[0],'200')) echo "<img src='$gravatar'>"; // OK
else if (strpos($headers[0],'404')) echo "No Gravatar"; // Not Found