我对PHP很陌生,所以如果你有任何想法或建议指出我正确的方向,我将不胜感激。
尝试制作一个简单的函数来检查用户的电子邮件地址是否转换为有效的Gravatar图像,但似乎gravatar.com已更改其标题。
使用get_headers('urlencoded_bad_email@example.com')
返回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"
}
P.S。我知道'&d'
参数,但它不符合我的目的。 :)
修改
使用'?d'
代替'&d'
。必须是gravatar.com'thang。
答案 0 :(得分:6)
Gravatar在“d”参数中添加了一个选项,意味着if you pass in d=404
,如果没有图片,则会获得404页面(而不是某些302
重定向到默认图片)必须使用启发式。
答案 1 :(得分:4)
注意:在撰写本文时,这是唯一的选择。但是,稍后添加了一些时间?d=404
,使Andrew's answer更加清晰。
虽然你说你知道d
parameter,但是你知道它实际上会在适用时返回一个重定向标题吗?所以,找到以下yields 302因为头像不存在:
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
在我看来,您需要做的就是添加d
参数并检查HTTP结果代码。
答案 2 :(得分:2)
我建议你试试LucasAraújo的php gravatar class。
/**
* 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>
答案 3 :(得分:1)
在检查重力图时,将“默认”参数添加到图像网址,如果找不到图像,则会提供302重定向。
$grav_url = 'http://www.gravatar.com/avatar/'.md5(mb_strtolower($email)).'?default=http://www.mysite.com/null.jpg&size=310';
如果你想要它,那么空图像可以返回404:)
答案 4 :(得分:1)
将Andrew Aylett的答案扩展为d = 404,实际上可以使用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
原始问题可以追溯到三年前。也许当时Gravatar的标题内容略有不同。
答案 5 :(得分:0)
“未找到/无效”Gravatar图片的文件名(Content-Disposition:inline; filename =“5ed352b75af7175464e354f6651c6e9e.jpg”)是否一致?如果是这样,您可以用它来识别无效图像吗?
答案 6 :(得分:-1)
真正无法解决的问题可能是将email
发布到http://en.gravatar.com/accounts/signup并检查Sorry, that email address is already used!
...
修改强>
好的,他们使用一些cookie来表明是否发生错误......; - )
function isUsed($email)
{
$url = 'http://en.gravatar.com/accounts/signup';
$email = strtolower($email);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'commit=Signup&email=' . urlencode($email));
curl_setopt($ch, CURLOPT_HEADER, true);
$response = curl_exec($ch);
curl_close($ch);
return (false !== strpos($response, 'Set-Cookie: gravatar-notices'));
}
var_dump(isUsed('test@example.org'));
答案 7 :(得分:-1)
一旦你得到它,不确定你想如何使用这个信息......但是你可以:
将图像加载到网页上,附加onload或onerror处理程序...如果onload触发,则表示匹配,如果onerror触发它不存在(或者加载它时出现问题)
e.g。
<img
src="http://www.gravatar.com/avatar/282eed17fcb9682bb2816697482b64ec?s=128&d=identicon&r=PG"
onload="itWorked();"
onerror="itFailed();"/>