2009-07-26 52 views
5

我很新的PHP,所以如果你有任何想法或建议,指向我在正确的方向,我会很感激。检查有效的gravatar(PHP)

试图做一个简单的函数来检查用户的电子邮件地址是否转换为有效的Gravatar图像,但似乎gravatar.com已更改其标题。

使用get_headers('[email protected]')回报的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" 
} 

ps我知道'&d'参数,但它不会达到我的目的。 :)

编辑:

使用'?d'而不是'&d'。必须是gravatar.com'thang。

+5

(只是旁注:按照RFC2607,在文档中请始终使用@ example.com,@ example.org或@ example.net - 不需要让address.com上的人收到垃圾邮件。) – Arjan 2009-07-26 21:07:21

+0

啊哈!我也知道。大声笑,固定。 – Jeff 2009-07-26 22:06:18

回答

4

注意:在写作的时候,这是唯一的选择。但是,一些后来的时间?d=404被添加,使得Andrew's answer更清洁。


虽然你说你知道的d parameter,你知道它实际上返回一个重定向头时是否适用?因此,下面yields 302发现,由于分身不存在:

http://www.gravatar.com/avatar/3b3be63a4c2a439b013787725dfce802?d=http%3A%2F%2Fwww.google.com%2Fimages%2Flogo.gif

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

我建议你试试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 = "[email protected]"; 
*  $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 <[email protected]> 
* @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 = '[email protected]'; 
$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> 
+1

我建议修改这个类来替换过时的(从5.3开始)调用eregi,并使用preg_match和/ i – hobodave 2009-07-26 21:01:10

+0

回复非常感谢,但我只需要一个函数来检查一个有效的gravatar图像。如果找不到有效的图像,则该函数需要返回FALSE。除非我忽略它,否则我没有在上面的类中看到代码。 – Jeff 2009-07-26 21:03:54

0

是文件名(内容处置:内联;文件名= “5ed352b75af7175464e354f6651c6e9e.jpg”)为 “没有找到/无效” 一致Gravatar图像?如果是这样,你可以用它来识别无效图像?

+0

我希望它是一致的,但每个电子邮件地址都不一样,无论它是否是有效的gravatar。 :( – Jeff 2009-07-26 21:20:30

+0

即使文件名不相同,内容是否与无效的电子邮件地址相同?也许您可以采用已知“无效”响应的MD5哈希值并将其用于比较... – 2009-07-26 21:26:15

+0

是的,内容相同对于无效的电子邮件地址,即使文件名不是,问题是,有效的回复与无效的回复相同 – Jeff 2009-07-26 22:08:36

-1

一个真正unperformant解决办法是张贴emailhttp://en.gravatar.com/accounts/signup和检查Sorry, that email address is already used! ...

编辑

好了,他们用一些饼干来表示,如果发生错误或不... ;-)

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('[email protected]')); 
-1

不知道你到底怎么想,一旦你得到它使用此信息...但是你能不能:

将图像加载到网页上,并附加了onload或onerror处理程序...如果onload激发,则您匹配,如果onerror激发它或者不存在(或者存在加载问题)

eg

<img 
    src="http://www.gravatar.com/avatar/282eed17fcb9682bb2816697482b64ec?s=128&d=identicon&r=PG" 
    onload="itWorked();" 
    onerror="itFailed();"/> 
1

检查gravatar时,将“default”参数添加到图片url,如果找不到图片,这将提供302重定向。

$grav_url = 'http://www.gravatar.com/avatar/'.md5(mb_strtolower($email)).'?default=http://www.mysite.com/null.jpg&size=310'; 

空图像可随后返回404,如果你希望它:)

6

的Gravatar增加了一个选项,以“d”参数,这意味着if you pass in d=404,你会得到一个404页(而不是一些302重定向到默认图片),如果没有图片,而不必使用启发式。

1

扩展由安德鲁·艾利特约d = 404答案,实际上有可能组成一个查询的gravatar与d=404(或default=404),然后看看标题,如果关键[0]包含一个值或。

$email = md5(strtolower("[email protected]")); 
$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的头文件有些不同。